问题描述
我使用嵌套的 LinearLayout
创建了以下布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo1"
android:layout_weight="1"
/>
<TextView
android:id="@+id/id_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo_value"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo2"
android:layout_weight="1"
/>
<TextView
android:id="@+id/id_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo2_value"
/>
</LinearLayout>
</LinearLayout>
这给出了以下内容:
a) 如何将此布局转换为使用 RelativeLayout
并获得相同的结果?
我尝试了以下方法:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo1"
android:layout_alignParentStart="true"
/>
<TextView
android:id="@+id/id_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo_value"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@id/id1"
/>
<TextView
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo2"
android:layout_below="@id/id1"
android:layout_alignParentStart="true"
/>
<TextView
android:id="@+id/id_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo2_value"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@id/id2"
android:layout_below="@id/id_1"
/>
</RelativeLayout>
但结果是:
我做错了什么?
b) 我如何转换为 ConstraintLayout
?我应该使用什么?
解决方法
a) 相对布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/id_1"
android:text="foo1" />
<TextView
android:id="@+id/id_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="foo_value" />
<TextView
android:id="@+id/id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/id1"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/id_2"
android:text="foo2" />
<TextView
android:id="@+id/id_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/id_1"
android:layout_alignParentEnd="true"
android:text="foo2_value" />
</RelativeLayout>
b) 约束布局
<?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">
<TextView
android:id="@+id/id1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="foo1"
app:layout_constraintEnd_toStartOf="@+id/id_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/id_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo_value"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/id2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="foo2"
app:layout_constraintEnd_toStartOf="@+id/id_2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id1" />
<TextView
android:id="@+id/id_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo2_value"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id1" />
</androidx.constraintlayout.widget.ConstraintLayout>