如何让 ConstraintLayout 中的视图根据另一个视图的可见性同时表现为 wrap_content 和 match_parent
How to have a view in ConstraintLayout behave as both wrap_content and match_parent depending on visibility of another view
正如您在下图中看到的,我正在寻找一种方法来拥有 2 个视图,并且我需要当两个视图都可见时,视图 2 表现为 wrap_parent,但是当视图 1没了,我就想把view 2拿走剩下的space.
我知道约束布局可以做到这一点;我还没找到办法。
这是我正在寻找的示例
编辑 1
这些视图都不是视图组。
将两个视图放入 spread_inside
的水平链中。将两个视图的宽度设置为 0dp
。对于 视图 2,指定 app:layout_constraintWidth_min="wrap"
。对于 View 1,将布局权重 (app:layout_constraintHorizontal_weight
) 设置为 1
。
以下XML为例:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:background="@android:color/holo_green_light"
android:text="View 1"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@android:color/holo_blue_light"
android:text="View 2"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_min="wrap" />
</androidx.constraintlayout.widget.ConstraintLayout>
当两个视图都可见时,我们会看到:
使用 View 1 gone
,我们看到:
正如您在下图中看到的,我正在寻找一种方法来拥有 2 个视图,并且我需要当两个视图都可见时,视图 2 表现为 wrap_parent,但是当视图 1没了,我就想把view 2拿走剩下的space.
我知道约束布局可以做到这一点;我还没找到办法。
这是我正在寻找的示例
编辑 1
这些视图都不是视图组。
将两个视图放入 spread_inside
的水平链中。将两个视图的宽度设置为 0dp
。对于 视图 2,指定 app:layout_constraintWidth_min="wrap"
。对于 View 1,将布局权重 (app:layout_constraintHorizontal_weight
) 设置为 1
。
以下XML为例:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:background="@android:color/holo_green_light"
android:text="View 1"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@android:color/holo_blue_light"
android:text="View 2"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_min="wrap" />
</androidx.constraintlayout.widget.ConstraintLayout>
当两个视图都可见时,我们会看到:
使用 View 1 gone
,我们看到: