android 中 Textview的底部文字对齐的两种方式

image.png

第一种使用RelativeLayout中的android:layout_alignBaseline="@id/text1"方法。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <TextView
        android:id="@+id/text1"
        android:text="20"
        android:background="#aa0000"
        android:textSize="55dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:text="小时"
        android:background="#00ff00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/text1"
        android:layout_alignBaseline="@+id/text1"
        />

</RelativeLayout>

第二种使用ConstraintLayout的app:layout_constraintBaseline_toBaselineOf="@+id/TextView1"方法

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <TextView
        android:id="@+id/text1"
        android:text="20"
        android:background="#aa0000"
        android:textSize="55dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
    <TextView
        android:text="小时"
        android:background="#00ff00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@+id/text1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBaseline_toBaselineOf="@+id/text1"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。