如何在 ConstraintLayout 中使用 LinearLayout 的权重?
How to have LinearLayout's weights in a ConstraintLayout?
我有以下布局,我希望在 LinearLayout
中具有与 weightSum
和 weight
相同的行为
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/linear_layout_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:background="#a4c639"
android:thelayout_constraintHorizontal_weight="5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main information"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#a4c639"
android:thelayout_constraintHorizontal_weight="5"
android:layout_marginStart="10dp"
app:layout_constraintTop_toTopOf="@id/linear_layout_1"
app:layout_constraintStart_toEndOf="@id/linear_layout_1"
app:layout_constraintTop_toBottomOf="@id/linear_layout_1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar"
android:thelayout_constraintHorizontal_weight="5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/linear_layout_2"
app:layout_constraintTop_toTopOf="@id/linear_layout_2"
app:layout_constraintBottom_toBottomOf="@id/linear_layout_2"
android:background="#a4c639"
/>
</android.support.constraint.ConstraintLayout>
它是这样显示的:
我希望所有 3 个小部件各占 space 的 1/3。我怎样才能做到这一点?
为了在 ConstraintLayout
中使用权重,您必须确保要加权的所有视图都形成一个 链 。在这种情况下,我们将处理 水平链 ,这意味着每个视图的开始 + 结束(或左 + 右)必须限制在其邻居或父视图中。
例如:
<View
android:id="@+id/first"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/second"
.../>
<View
android:id="@+id/second"
app:layout_constraintStart_toEndOf="@id/first"
app:layout_constraintEnd_toStartOf="@id/third"
.../>
<View
android:id="@+id/third"
app:layout_constraintStart_toEndOf="@id/second"
app:layout_constraintEnd_toEndOf="parent"
.../>
如果你想让视图均匀分布,那么每个视图的宽度应该使用MATCH_CONSTRAINTS
(0dp
):
<View
android:id="@+id/first"
android:layout_width="0dp"
.../>
<View
android:id="@+id/second"
android:layout_width="0dp"
.../>
<View
android:id="@+id/third"
android:layout_width="0dp"
.../>
如果您想更改视图的加权方式,则必须指定每个视图的权重(除了将宽度设置为 0dp
之外)。在这里,中间视图将是两侧视图的两倍:
<View
android:id="@+id/first"
app:layout_constraintHorizontal_weight="1"
.../>
<View
android:id="@+id/second"
app:layout_constraintHorizontal_weight="2"
.../>
<View
android:id="@+id/third"
app:layout_constraintHorizontal_weight="1"
.../>
我有以下布局,我希望在 LinearLayout
weightSum
和 weight
相同的行为
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/linear_layout_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:background="#a4c639"
android:thelayout_constraintHorizontal_weight="5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main information"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#a4c639"
android:thelayout_constraintHorizontal_weight="5"
android:layout_marginStart="10dp"
app:layout_constraintTop_toTopOf="@id/linear_layout_1"
app:layout_constraintStart_toEndOf="@id/linear_layout_1"
app:layout_constraintTop_toBottomOf="@id/linear_layout_1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar"
android:thelayout_constraintHorizontal_weight="5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/linear_layout_2"
app:layout_constraintTop_toTopOf="@id/linear_layout_2"
app:layout_constraintBottom_toBottomOf="@id/linear_layout_2"
android:background="#a4c639"
/>
</android.support.constraint.ConstraintLayout>
它是这样显示的:
我希望所有 3 个小部件各占 space 的 1/3。我怎样才能做到这一点?
为了在 ConstraintLayout
中使用权重,您必须确保要加权的所有视图都形成一个 链 。在这种情况下,我们将处理 水平链 ,这意味着每个视图的开始 + 结束(或左 + 右)必须限制在其邻居或父视图中。
例如:
<View
android:id="@+id/first"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/second"
.../>
<View
android:id="@+id/second"
app:layout_constraintStart_toEndOf="@id/first"
app:layout_constraintEnd_toStartOf="@id/third"
.../>
<View
android:id="@+id/third"
app:layout_constraintStart_toEndOf="@id/second"
app:layout_constraintEnd_toEndOf="parent"
.../>
如果你想让视图均匀分布,那么每个视图的宽度应该使用MATCH_CONSTRAINTS
(0dp
):
<View
android:id="@+id/first"
android:layout_width="0dp"
.../>
<View
android:id="@+id/second"
android:layout_width="0dp"
.../>
<View
android:id="@+id/third"
android:layout_width="0dp"
.../>
如果您想更改视图的加权方式,则必须指定每个视图的权重(除了将宽度设置为 0dp
之外)。在这里,中间视图将是两侧视图的两倍:
<View
android:id="@+id/first"
app:layout_constraintHorizontal_weight="1"
.../>
<View
android:id="@+id/second"
app:layout_constraintHorizontal_weight="2"
.../>
<View
android:id="@+id/third"
app:layout_constraintHorizontal_weight="1"
.../>