1

海兰,约束布局隐藏查看与链重量

我想有2次平均分布在屏幕的宽度在ConstraintLayout。 用链条和重量来实现。

<Button 
    android:id="@+id/buttonLeft" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="0dp" 
    android:layout_marginRight="0dp" 
    android:text="Left" 
    app:layout_constraintHorizontal_chainStyle="spread_inside" 
    app:layout_constraintHorizontal_weight="1" 
    app:layout_constraintLeft_toLeftOf="@+id/guidelineLeft" 
    app:layout_constraintRight_toLeftOf="@+id/buttonRight" 
    tools:layout_editor_absoluteY="0dp"/> 

<Button 
    android:id="@+id/buttonRight" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="0dp" 
    android:text="Right" 
    app:layout_constraintHorizontal_weight="1" 
    app:layout_constraintLeft_toRightOf="@+id/buttonLeft" 
    app:layout_constraintRight_toLeftOf="@+id/guidelineRight" 
    tools:layout_editor_absoluteY="0dp"/> 

现在我想隐藏右边的视图和左边的增长来填充宽度。

我想这可能因为visiblity behaviour

开箱的,但作为左按钮的右边都有一个约束,它不会增长。

任何想法如何解决?

回答

3

隐藏右视图的工作原理:

<?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" 
    xmlns:tools="http://schemas.android.com/tools"> 

    <Button 
     android:id="@+id/buttonLeft" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="0dp" 
     android:layout_marginRight="0dp" 
     android:text="Left" 
     app:layout_constraintHorizontal_chainStyle="spread_inside" 
     app:layout_constraintHorizontal_weight="1" 
     app:layout_constraintLeft_toLeftOf="@+id/guidelineLeft" 
     app:layout_constraintRight_toLeftOf="@+id/buttonRight" 
     tools:layout_editor_absoluteY="0dp"/> 

    <Button 
     android:id="@+id/buttonRight" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="0dp" 
     android:text="Right" 
     android:visibility="gone" 
     app:layout_constraintHorizontal_weight="1" 
     app:layout_constraintLeft_toRightOf="@+id/buttonLeft" 
     app:layout_constraintRight_toLeftOf="@+id/guidelineRight" 
     tools:layout_editor_absoluteY="0dp"/> 

    <android.support.constraint.Guideline 
     android:id="@+id/guidelineLeft" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintGuide_begin="20dp" 
     android:orientation="vertical" /> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guidelineRight" 
     android:orientation="vertical" 
     app:layout_constraintGuide_end="70dp" /> 

</android.support.constraint.ConstraintLayout> 

...但隐藏左视图没有。我在这里提出了一个错误:https://code.google.com/p/android/issues/detail?id=234897

+0

感谢您的快速回答。但如果我使用您提出的布局,左侧视图不会在右侧隐藏时增长。至少布局编辑器不显示它。看到[这里](https://imgur.com/a/TpKD4) – cwiesner

+1

现在可以使用1.0.1中包含的bug修复 - thx – cwiesner