我在约束布局中有几个小组,我用它来显示/隐藏其组件。但是然后我注意到我可以将约束添加到组(在“属性”中看到“约束”小组件)。我尝试调整组的大小,在Attributes处添加约束,但未做任何更改。
我在网上找到的唯一关于分组的信息是,我可以显示/隐藏其组成部分。即使根据https://developer.android.com/reference/android/support/constraint/Group,似乎也没有可能向组添加约束。
我的问题是,是否可以向该组添加约束,而不是向每个视图添加约束?
这里提供一个示例,您可以在此处查看如何限制视图组的工作。尝试使用此布局进行任何演示活动,以检查其功能。在这里,您可以看到我为视图组设置了visibility="gone"
,并为Add
和Done
按钮设置了参考。因此对于子项add
和done
按钮将设置为visibility="gone"
。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:gravity="center"
android:orientation="vertical">
<androidx.constraintlayout.widget.Group
android:layout_width="wrap_content"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:constraint_referenced_ids="btnAdd,btnDone" />
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="No buttons visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
app:layout_constraintEnd_toStartOf="@+id/btnAdd"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>