在ConstraintLayout中对视图进行分组以将它们视为单个视图

Yur*_*rov 30 android android-studio android-constraintlayout constraint-layout-chains

我需要对一组视图应用一些约束ConstraintLayout.我想将这些视图分组并继续编辑,而Android Studio中的布局设计器将它们视为单个视图.有没有办法这样做而不用ViewGroup(另一种布局)实际包装视图?如果需要这样的包装器,可能会有一个包装器布局,ConstraintLayout并允许对对象进行分组而不会创建像RelativeLayout

Yur*_*rov 32

ConstraintLayout Chains

Android开发人员最近发布了一个新版本ConstraintLayout(截至今天的1.0.2).此版本包含一个新的主要功能 - ,允许我们将视图分组ConstraintLayout.

链在单个轴(水平或垂直)上提供类似组的行为.

如果一组小部件通过双向连接链接在一起,则它们被视为链

创建链后,有两种可能性:

  • 在可用空间中传播元素
  • 链也可以"打包",在这种情况下,元素被组合在一起

目前,您需要使用以下gradle依赖项来使用此功能(因为它是alpha):

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

在这里,您可以找到ConstraintLayout要在项目中使用的最新版本.

Android Studio 2.3之前,Android Studio用户界面设计器不支持创建链,因为您无法在其中添加双向约束.解决方案是在TranslucentCloud中提到的手动XML中创建这些约束.从Android Studio 2.3(目前仅在canary频道上),UI编辑器中也支持链(如评论中提到的GoRoS).


以下是如何使用ConstraintLayout在屏幕中间将两个视图放在一起的示例:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5"
        app:layout_constraintVertical_chainPacked="true"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

@Mateus Gondim更新(2018年1月)

在最近的版本中,您应该使用app:layout_constraintVertical_chainStyle="packed"而不是app:layout_constraintVertical_chainPacked="true"


  • 需要添加,在Android Studio 2.2.2中,不可能通过用户界面设计器进行双向连接,因此需要直接在XML资源中指定它们. (3认同)
  • 现在你应该使用`app:layout_constraintVertical_chainStyle ="packed"`而不是`app:layout_constraintVertical_chainPacked ="true"`.我在'约束布局:1.0.2'`版本,后者不再工作了. (2认同)

Von*_*zer 9

您可以使用

android.support.constraint.Guideline
Run Code Online (Sandbox Code Playgroud)

将元素组合在一起.

添加指南(垂直或水平),然后将其用作其他视图的锚点.以下是水平居中两个分组文本视图的简单示例:(在AS中显示设计视图)

在此输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<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="56dp"
    android:background="@android:color/white"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">


    <TextView
        android:id="@+id/top_text"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        android:text="Above"
        tools:text="Above" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <TextView
        android:id="@+id/bottom_text"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        android:textSize="16sp"
        app:layout_constraintTop_toBottomOf="@+id/guideline"
        android:text="Below"
        tools:text="Below" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Right vertically centered"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Right vertically centered"/>
</ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

  • 如果有三个textView要垂直对齐怎么办 (2认同)

Hon*_*ong 5

我只想在公认的出色答案中添加一些图形。这非常简单:

在布局设计视图中 > 选择要分组的所有视图 > 右键单击​​它们 > 链 > 创建水平链: 在此处输入图片说明

将它们居中也很简单:

选择组的头部视图 > Organize > Pack Horizo​​ntally > 将第一个视图的开始约束和最后一个视图的结束约束设置为它们的父视图: 在此处输入图片说明