【问题标题】:How to group multiple views in a ConstraintLayout如何在一个 ConstraintLayout 中对多个视图进行分组
【发布时间】:2017-02-08 16:34:19
【问题描述】:

我在 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" />

【问题讨论】:

标签: android performance android-layout android-linearlayout android-constraintlayout


【解决方案1】:

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

处理特定视图组的可见性

这是 ConstraintLayout 中引入的新功能,目前 在测试版中。

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

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

allprojects {
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
}

然后在app 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,因此在活动中您只需将 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

编辑:ConstraintLayout 1.1.0 稳定版于 2018 年 4 月 12 日发布 https://androidstudio.googleblog.com/2018/04/constraintlayout-110.html

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

编辑:Android X 如果有人使用 android x 包,你可以在这里找到包信息

https://developer.android.com/jetpack/androidx/migrate

并使用:

<androidx.constraintlayout.widget.Group />

【讨论】:

  • 是否可以更改组的背景颜色?
  • @IbrahimDisouki 我认为它目前不存在,如果我发现任何东西我会在这里更新。
  • 不可能。创建组是为了控制其成员的可见性,而不是视图定位或类似的东西。
  • 是的,因为组参考视图位置可以在屏幕上的任何位置,所以从技术上讲它没有任何意义
  • 可以在不属于约束布局的视图上使用androidx.constraintlayout.widget.Group 吗?
【解决方案2】:

目前您无法做到这一点。您必须单独禁用每个按钮,因为约束被添加到约束布局中的每个小部件。

要对视图进行分组,您需要使用视图组,这在约束布局的上下文中没有意义。

编辑

使用约束布局:1.1.0-beta1,您可以使用android.support.constraint.Group 对视图进行分组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2023-02-14
    相关资源
    最近更新 更多