我正在尝试采用多个水平链接的按钮和 TextView 并将它们垂直链接为视图集,但仍保持平面视图层次结构。这是我的初始布局和代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnTopLeft"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#eeeeee"
app:layout_constraintBottom_toTopOf="@+id/lblTopLeft"
app:layout_constraintEnd_toStartOf="@+id/btnTopRight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="1"
tools:textSize="42sp" />
<TextView
android:id="@+id/lblTopLeft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/btnMiddleLeft"
app:layout_constraintEnd_toEndOf="@id/btnTopLeft"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/btnTopLeft"
app:layout_constraintTop_toBottomOf="@+id/btnTopLeft"
tools:text="Button 1" />
<Button
android:id="@+id/btnTopRight"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#dddddd"
app:layout_constraintBottom_toTopOf="@+id/lblTopRight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnTopLeft"
app:layout_constraintTop_toTopOf="parent"
tools:text="2"
tools:textSize="42sp" />
<TextView
android:id="@+id/lblTopRight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/btnMiddleRight"
app:layout_constraintEnd_toEndOf="@id/btnTopRight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/btnTopRight"
app:layout_constraintTop_toBottomOf="@+id/btnTopRight"
tools:text="Button 2" />
<Button
android:id="@+id/btnMiddleLeft"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#cccccc"
app:layout_constraintBottom_toTopOf="@+id/lblMiddleLeft"
app:layout_constraintEnd_toStartOf="@+id/btnMiddleRight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lblTopLeft"
tools:text="3"
tools:textSize="42sp" />
<TextView
android:id="@+id/lblMiddleLeft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/btnBottomLeft"
app:layout_constraintEnd_toEndOf="@id/btnMiddleLeft"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/btnMiddleLeft"
app:layout_constraintTop_toBottomOf="@+id/btnMiddleLeft"
tools:text="Button 3" />
<Button
android:id="@+id/btnMiddleRight"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#bbbbbb"
app:layout_constraintBottom_toTopOf="@+id/lblMiddleRight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnMiddleLeft"
app:layout_constraintTop_toBottomOf="@+id/lblTopRight"
tools:text="4"
tools:textSize="42sp" />
<TextView
android:id="@+id/lblMiddleRight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/btnBottomRight"
app:layout_constraintEnd_toEndOf="@id/btnMiddleRight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/btnMiddleRight"
app:layout_constraintTop_toBottomOf="@+id/btnMiddleRight"
tools:text="Button 4" />
<Button
android:id="@+id/btnBottomLeft"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#aaaaaa"
app:layout_constraintBottom_toTopOf="@+id/lblBottomLeft"
app:layout_constraintEnd_toStartOf="@+id/btnBottomRight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lblMiddleLeft"
tools:text="5"
tools:textSize="42sp" />
<TextView
android:id="@+id/lblBottomLeft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/btnBottomLeft"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/btnBottomLeft"
app:layout_constraintTop_toBottomOf="@+id/btnBottomLeft"
tools:text="Button 5" />
<Button
android:id="@+id/btnBottomRight"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#999999"
app:layout_constraintBottom_toTopOf="@+id/lblBottomRight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnBottomLeft"
app:layout_constraintTop_toBottomOf="@+id/lblMiddleRight"
tools:text="6"
tools:textSize="42sp" />
<TextView
android:id="@+id/lblBottomRight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/btnBottomRight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@id/btnBottomRight"
app:layout_constraintTop_toBottomOf="@+id/btnBottomRight"
tools:text="Button 6" />
</android.support.constraint.ConstraintLayout>
当我设置按钮及其 TextView 的可见性时,我希望同一行上的按钮填充剩余空间,但我希望它下方或上方的按钮保持在同一位置,直到该行上的两个按钮都有被隐藏了。隐藏同一行上的两个按钮后,我希望其余按钮填充剩余空间。例如,当我隐藏 Button 和 TextView 3 时,我希望布局看起来像这样:
但是目前如果我隐藏 Button 和 TextView 3,这就是当前发生的情况:
这是基于我当前代码的预期行为,但我正在寻找一种方法来防止垂直链向下折叠,直到按钮 4 也被隐藏。隐藏按钮 4 后,这是所需的结果:
我玩过障碍物,但在任何一点放置障碍物似乎都会导致按钮的动态高度复杂化,因为它会破坏垂直链。谁能提供一些关于如何通过平面视图层次结构实现这一点的见解?