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

问题描述 投票:0回答:5

我需要对

ConstraintLayout
中的一组视图应用一些约束。我想对这些视图进行分组并继续编辑,而 Android studio 中的布局设计器将它们视为单个视图。有没有办法做到这一点,而无需实际用
ViewGroup
(另一种布局)包装视图?如果这样的包装器是必要的,也许有一个包装器布局,它带有
ConstraintLayout
并允许对对象进行分组,而无需创建像
RelativeLayout
这样的繁重布局?

android android-studio android-constraintlayout constraint-layout-chains
5个回答
50
投票

约束布局链

Android 开发人员最近发布了新版本

ConstraintLayout
(截至今天为 1.0.2)。该版本包含一个新的主要功能 - Chains,它允许我们对
ConstraintLayout
中的视图进行分组。

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

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

一旦创建了一条链,就有两种可能性:

  • 将元素分散在可用空间中
  • 链也可以“打包”,在这种情况下元素被分组在一起

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

 compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9'

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

ConstraintLayout

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


示例

以下是如何使用

ConstraintLayout
chains 将两个视图一起放置在屏幕中间的示例:

<?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>

更新(2018 年 1 月),作者:@Mateus Gondim

在最近的版本中,您应该使用

app:layout_constraintVertical_chainStyle="packed"
而不是
app:layout_constraintVertical_chainPacked="true"



11
投票

你可以使用

android.support.constraint.Guideline

将元素组合在一起。

添加参考线(垂直或水平),然后将其用作其他视图的锚点。这是水平居中两个分组文本视图的简单示例:(在 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>

8
投票

我只是想在已被广泛接受的答案中添加一些图形。非常简单:

在布局设计视图中>选择要分组的所有视图>右键单击它们>链>创建水平链:

将它们居中也很简单:

选择组的head视图>组织>水平打包>将第一个视图的开始约束和最后一个视图的结束约束设置为其父视图:


-1
投票

您可以使用高度0dp组织视图垂直

相对布局中,我们可以使用下面和上面的布局

<FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_nav"
        android:layout_below="@+id/toolbar"
        />

在约束布局中,给出高度是强制性的,因此您可以给出 0 高度。 然后您可以将您的视图设置在其他视图下方,如下所示

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tablayout" />

您可以使用 constraintTop_toBottomOfconstraintBottom_toTopOf 属性将您的视图调整到其他视图的上方或下方。 谢谢


© www.soinside.com 2019 - 2024. All rights reserved.