【问题标题】:Group views in ConstraintLayout to treat them as a single view在 ConstraintLayout 中对视图进行分组以将它们视为单个视图
【发布时间】:2016-05-29 07:47:08
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    约束布局链

    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 用户界面设计器不支持创建链,因为您无法在其中添加双向约束。如TranslucentCloud 所述,解决方案是在手动XML 中创建这些约束。从 Android Studio 2.3(目前仅在 canary 通道上)开始,UI 编辑器也支持链(如 cmets 中提到的 GoRoS)。


    示例

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

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


    【讨论】:

    • 需要补充,在Android Studio 2.2.2中无法通过用户界面设计器进行双向连接,需要直接在XML资源中指定。
    • 然而,目前在金丝雀频道中的新 Android Studio 2.3 确实支持布局编辑器视图中的链 tools.android.com/recent/androidstudio23canaryavailable
    • 现在您应该使用app:layout_constraintVertical_chainStyle="packed" 而不是app:layout_constraintVertical_chainPacked="true"。我在constraint-layout:1.0.2' 版本上,后者不再工作了。
    • 不是他问的。
    【解决方案2】:

    你可以使用

    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>
    

    【讨论】:

    • 如果有三个textViews要垂直对齐怎么办
    【解决方案3】:

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

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

    将它们居中也很简单:

    选择组的head视图 > Organize > Pack Horizo​​ntally > 将第一个视图的开始约束和最后一个视图的结束约束设置为其父视图:

    【讨论】:

      【解决方案4】:

      您可以使用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 属性将您的视图调整为高于或低于其他视图。 谢谢

      【讨论】:

      • 虽然这是很好的信息,但我不认为这是 OP 的要求。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多