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>
网友评论