ConstraintLayout问题与水平权重

时间:2017-07-01 09:11:00

标签: android android-layout android-constraintlayout

考虑遵循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>

产生这个我想要的布局:

enter image description here

但如果我将B3标记为已消失,则B1的宽度设为0,B2向左移动。我希望B2能够占据B3的位置,而B1则需要扩展:

enter image description here

知道我可能做错了吗?

1 个答案:

答案 0 :(得分:5)

为B2设置app:layout_constraintHorizontal_weight="1"app:layout_constraintRight_toLeftOf="@id/b3",你就可以了。

编辑:只需从B2中删除disable when