gpt4 book ai didi

android - 从具有平面 View 层次 ConstraintLayout 的多个水平链创建垂直链

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:53 27 4
gpt4 key购买 nike

我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:

Initial Layout

<?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>

当我设置 Button 及其 TextView 的可见性时,我希望同一行上的按钮填充剩余空间,但我希望它下方或上方的按钮保持在同一位置,直到 行上的两个 按钮都已隐藏。一旦同一行上的两个按钮都被隐藏,那么我希望剩余的按钮填充剩余的空间。例如,当我隐藏 Button 和 TextView 3 时,我希望布局看起来像这样:

Correct Layout

但是目前,如果我隐藏 Button 和 TextView 3,则会发生以下情况:

Incorrect Layout

这是基于我当前代码的预期行为,但我正在寻找一种方法来防止垂直链向下折叠,直到按钮 4 也被隐藏。一旦按钮 4 被隐藏,这就是期望的结果:

Correct Layout 2

我玩过 Barriers,但在任何一点放置障碍似乎都会导致按钮的动态高度复杂化,因为它会打断垂直链。谁能提供一些关于如何使用平面 View 层次结构实现此目的的见解?

最佳答案

将屏幕分成三个垂直链接的 View ,如下所示:

enter image description here

我设置了背景颜色以使 View 更加突出,但在实际实现中它们可能是透明的或设置为不可见

现在,添加按钮和标签,使它们垂直约束在这些 View 的内部。这确实打破了垂直链,但垂直尺寸现在由底层 View 的垂直链控制。按钮和标签仍然链接到 ConstraintLayout 的两侧。

enter image description here

按钮和标签仍然像以前一样左右调整大小。但是,现在,在一行中的所有 View 都设置为“消失”之前,下面的按钮不会展开。

enter image description here

当一行中的所有按钮和标签也设置为“消失”时,您需要将基础 View 设置为“消失”,因此您需要跟踪一行中按钮和标签的状态。

这是 XML:

activity_main.xml

<android.support.constraint.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="match_parent">

<View
android:id="@+id/topGroup"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_green_light"
app:layout_constraintBottom_toTopOf="@id/centerGroup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<View
android:id="@+id/centerGroup"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_light"
app:layout_constraintBottom_toTopOf="@id/bottomGroup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/topGroup" />

<View
android:id="@+id/bottomGroup"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_orange_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/centerGroup" />

<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="@id/topGroup"
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_toBottomOf="@+id/topGroup"
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="@id/topGroup"
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_toBottomOf="@+id/topGroup"
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_toTopOf="@+id/centerGroup"
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_toBottomOf="@+id/centerGroup"
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_toTopOf="@+id/centerGroup"
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_toBottomOf="@+id/centerGroup"
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_toTopOf="@id/bottomGroup"
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="@id/bottomGroup"
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_toTopOf="@+id/bottomGroup"
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="@id/bottomGroup"
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>

关于android - 从具有平面 View 层次 ConstraintLayout 的多个水平链创建垂直链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56228452/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com