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