Android ConstraintLayout - 如何将视图移动到占用 space?

Android ConstraintLayout - How to move view to occupied space?

请看这张图片,这是我目前创建的:

但我的问题是在某些时候颜色段可能变得不可见或消失。而此时我需要将尺寸段移动到与颜色段相同的位置。我怎样才能做到这一点 ?基本上,在 invisible/gone.

的情况下,我希望尺寸段代替颜色段

我尝试创建一个指南,但它是不变的,不会移动。我想知道障碍是否有效?

我可以想到两种方法。一个只使用 XML 但不太灵活,第二个需要对代码进行一些更改并提供对 Views 的更多控制。请注意,只有当颜色段的可见性设置为 GONE 时,这两种方法才有效,因为 INVISIBLE 的视图仍然占据 space.

  1. 我们将两个视图的宽度百分比设置为 50%。当颜色 segment is GONE Size 段将向左滑动,因为 水平偏差。如果您设置此解决方案可能无法正常工作 这些视图的边距,因为它们的大小固定为 50% 布局的可用宽度。

    <?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">
    
        <TextView
            android:id="@+id/color"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Color"
            app:layout_constraintWidth_percent="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0"/>
    
        <TextView
            android:id="@+id/size"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Size"
            app:layout_constraintWidth_percent="0.5"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/color"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0"/>
    
    </android.support.constraint.ConstraintLayout>
    
  2. 我们将两个视图放在一个加权链中,但还添加了一个 Space 助手 当 'Color' 时,它充当剩余 space 的填充物 视图是 GONE。可见的视图都具有权重 0.5,各占一半space。 Space 助手的权重为 0,因此不可见。

    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"
        android:id="@+id/layout">
    
        <TextView
            android:id="@+id/color"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Color"
            app:layout_constraintEnd_toStartOf="@id/size"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintHorizontal_weight="0.5"/>
    
        <TextView
            android:id="@+id/size"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Size"
            app:layout_constraintEnd_toStartOf="@id/space"
            app:layout_constraintStart_toEndOf="@id/color"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_weight="0.5"/>
    
        <Space
            android:id="@+id/space"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/size"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_weight="0"/>
    
    </android.support.constraint.ConstraintLayout>
    

    现在当你需要隐藏你的颜色段时,你会做一些事情 像这样:

    ConstraintSet cs = new ConstraintSet();
    ConstraintLayout layout = findViewById(R.id.layout);
    Space space = findViewById(R.id.space);
    TextView color = findViewById(R.id.color);
    cs.clone(layout);
    cs.setHorizontalWeight(space.getId(), 0.5f);
    cs.applyTo(layout);
    color.setVisibility(View.GONE);
    

    这将使颜色部分消失,但尺寸部分消失 将向左滑动,因为约束甚至被保留 然后视图的可见性设置为 GONESpace 有它的 权重设置为 0.5 并将占据 space.

  3. 的右半部分