水平权重的 ConstraintLayout 问题
ConstraintLayout issue with horizontal weight
考虑以下 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<Button
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="B1"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/b2"/>
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B2"
app:layout_constraintLeft_toRightOf="@+id/b1"
app:layout_constraintRight_toLeftOf="@id/b3"/>
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B3"
app:layout_constraintLeft_toRightOf="@+id/b2"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
生成我想要的布局:
但是如果我将 B3 标记为消失,则 B1 的宽度设置为 0,B2 向左移动。而我希望 B2 取代 B3 的位置并扩展 B1:
知道我做错了什么吗?
为 B2 设置 android:layout_width = "0dp"
和 app:layout_constraintHorizontal_weight="1"
,您就可以开始了。
编辑:只需从 B2 中删除 app:layout_constraintRight_toLeftOf="@id/b3"
。
考虑以下 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<Button
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="B1"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/b2"/>
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B2"
app:layout_constraintLeft_toRightOf="@+id/b1"
app:layout_constraintRight_toLeftOf="@id/b3"/>
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B3"
app:layout_constraintLeft_toRightOf="@+id/b2"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
生成我想要的布局:
但是如果我将 B3 标记为消失,则 B1 的宽度设置为 0,B2 向左移动。而我希望 B2 取代 B3 的位置并扩展 B1:
知道我做错了什么吗?
为 B2 设置 android:layout_width = "0dp"
和 app:layout_constraintHorizontal_weight="1"
,您就可以开始了。
编辑:只需从 B2 中删除 app:layout_constraintRight_toLeftOf="@id/b3"
。