如何在ConstraintLayout中对多个视图进行分组

dev*_*v90 18 performance android android-layout android-linearlayout android-constraintlayout

我在ConstraintLayout中添加了3个按钮.我添加了一个按钮来禁用或启用这些按钮.

如果我使用普通的LinearLayout.我可以将所有按钮放在线性布局中,并启用或禁用该特定布局.

但我正在使用ConstraintLayout.所以我需要禁用或启用所有这些按钮,我相信ConstraintLayout必须有一种方法来组合不同的视图.

请指导我如何在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" />
Run Code Online (Sandbox Code Playgroud)

Pav*_*van 45

是的,正如我所知,您可以使用线性布局处理可见性,但不能启用/禁用视图我认为,如果我错了,请纠正我.所以现在在ConstraintLayout中我们也可以使用Group处理特定视图组的可见性

这是ConstraintLayout中引入的新功能,目前处于Beta版本.

如何将beta ConstraintLayout添加到项目中,请按照以下步骤操作

在项目gradle文件中添加maven支持,如下所示

allprojects {
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在app gardle依赖项中添加ConstarintLayout库依赖项

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
Run Code Online (Sandbox Code Playgroud)

现在您必须在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" />  
Run Code Online (Sandbox Code Playgroud)

在组参考ID中的位置

app:constraint_referenced_ids="button7,button3,button2"
Run Code Online (Sandbox Code Playgroud)

包含要处理运行时逗号分隔视图ID,因此在活动中,您只需按如下方式绑定Group并处理可见性

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
Run Code Online (Sandbox Code Playgroud)

编辑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'

  • 是否可以更改组的背景颜色? (3认同)
  • 我上面的问题的答案是,不。此外,您不能只将“androidx.constraintlayout.widget.Group”放置在布局文件中的任何位置,它必须与它引用的视图共享相同的父级“androidx.constraintlayout.widget.ConstraintLayout”。 (2认同)