如何在ConstraintLayout中对多个视图进行分组 [英] How to group multiple views in a ConstraintLayout
问题描述
我在ConstraintLayout中添加了3个按钮.我添加了一个按钮来禁用或启用这些按钮.
I have added 3 buttons in a ConstraintLayout. I have added a button to disable or enable these buttons.
如果我使用的是正常的LinearLayout.我本可以将所有按钮放在线性布局"中,然后启用或禁用该特定布局.
If I was using normal LinearLayout. I could have put all the buttons in a Linear Layout and enable or disable that particular layout.
但是我正在使用ConstraintLayout.因此,我需要禁用或启用所有这些按钮,我认为ConstraintLayout中必须存在一种将不同的视图分组的方法.
But I am using ConstraintLayout. So I need to disable or enable all of these buttons, I believe that there must be a way in ConstraintLayout to group different views.
请指导我如何在ConstriantLayout中对视图进行分组
Kindly guide me how to group views in ConstriantLayout
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toRightOf="@+id/button"
android:layout_marginLeft="8dp"
app:layout_constraintTop_toTopOf="@+id/button" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
app:layout_constraintTop_toTopOf="@+id/button2"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toRightOf="@+id/button2"
android:layout_marginLeft="8dp" />
推荐答案
是的,据我所知,您可以使用线性布局来处理可见性,但不能启用/禁用视图,如果我错了,请纠正我.因此,现在在 ConstraintLayout 中,我们也可以使用 Group
Yes, as I know you can handle visibility using linear layout but not Enable/Disable views I think, correct me if I am wrong. So now in ConstraintLayout also we can handle visibility of particular group of views using the Group
这是ConstraintLayout中引入的新功能,目前 在Beta版中.
This is new feature introduced in ConstraintLayout which is currently in Beta version.
如何将beta ConstraintLayout添加到项目中,请按照以下步骤操作
How to add beta ConstraintLayout to project follow steps below
如下所示在项目gradle文件中添加maven支持
allprojects {
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
}
然后在应用程序gardle依赖性中添加ConstarintLayout库依赖性
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
现在您必须按照以下说明在ConstraintLayout中添加组
<android.support.constraint.Group
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="button7,button3,button2"
android:id="@+id/group" />
组参考ID中的位置
app:constraint_referenced_ids="button7,button3,button2"
包含要处理运行时的逗号分隔视图ID ,因此在活动中,您只需按如下所示绑定组并处理可见性
contains the comma separated view id's you want to handle run time, so in activity you just bind Group as below and handle visibility
import android.support.constraint.Group; //import statement in activity
Group group=(Group)findViewById(R.id.group);//bind view from xml
group.setVisibility(View.VISIBLE);//this will visible all views
group.setVisibility(View.GONE);//this will set Gone to all views
group.setVisibility(View.INVISIBLE);//this will set INVISIBLE to all view
EDIT ConrtsaintLayout 1.1.0稳定版于2018年4月12日发布 https://androidstudio.googleblog.com/2018/04/constraintlayout-110.html
实现'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
编辑Android X 如果有人使用android x软件包,则可以在此处找到软件包信息
Edit Android X If anyone using android x package you can find package info here
https://developer.android.com/jetpack/androidx/migrate
这篇关于如何在ConstraintLayout中对多个视图进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!