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

时间:2016-05-29 07:47:09

标签: android android-studio android-constraintlayout constraint-layout-chains

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

4 个答案:

答案 0 :(得分:27)

ConstraintLayout Chains

Android开发者最近发布了ConstraintLayout的新版本(截至今天, 1.0.2 )。此版本包含一个新的主要功能 - Chains,可让我们在ConstraintLayout中对视图进行分组。

  

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

     

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

     

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

     
      
  • 在可用空间中展开元素
  •   
  • 链也可以“打包”,在这种情况下,元素组合在一起
  •   

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

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

Here您可以在项目中找到最新版本的ConstraintLayout

直到Android Studio 2.3,Android Studio用户界面设计器不支持创建链,因为您无法在其中添加双向约束。解决方案是在手动XML中创建这些约束,如TranslucentCloud所述。从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>

@Mateus Gondim

更新(2018年1月)

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

答案 1 :(得分:9)

您可以使用

android.support.constraint.Guideline

将元素组合在一起。

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

enter image description here

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

答案 2 :(得分:1)

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

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

将它们居中也很简单:

选择组的 head 视图>组织>横向包装>将第一个视图的开始约束和最后一个视图的结束约束设置为其父级: enter image description here

答案 3 :(得分:-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_toBottomOf constraintBottom_toTopOf 属性将视图调整为其他视图的上方或下方。 谢谢

相关问题