使用ConstraintLayout均匀地分隔视图

LinearLayout一个常见用途是平均间隔(重量)视图,例如: 示例布局

你如何使用新的ConstraintLayout实现这样的均匀间隔的视图?

ConstraintLayout链接供参考: 博客文章 , I / O会话video

有两种方法可以使用ConstraintLayout来完成: 链和指南 。 要使用Chains,请确保您使用的是ConstraintLayout Beta 3或更高版本,如果您要使用Android Studio中的可视布局编辑器,请确保您使用的是Android Studio 2.3 Beta 1或更高版本。

方法1 – 使用链

打开布局编辑器并按照常规添加小部件,根据需要添加父级约束。 在这种情况下,我添加了两个带有约束的button,父对象的父项和父对象的左侧(“保存”button的左侧,“分享”button的右侧):

在这里输入图像说明

请注意,在这种状态下,如果我翻转到横向视图,视图不会填充父项,而是锚定在angular落:

在这里输入图像说明

突出显示两个视图,通过Ctrl / Cmd单击或通过在视图周围拖动一个框:

在这里输入图像说明

然后右键单击视图并select“水平居中”:

在这里输入图像说明

这build立了视图之间的双向连接(链是如何定义的)。 默认情况下,链式是“传播”,即使没有包含XML属性,也可以应用。 使用这种链式样式,但将视图的宽度设置为0dp ,则视图将填充可用空间,并在父级之间均匀分布:

在这里输入图像说明

在景观上更为明显:

在这里输入图像说明

如果您不想跳过布局编辑器,则生成的XML将如下所示:

 <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"> <Button android:id="@+id/button_save" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/button_save_text" android:layout_marginStart="8dp" android:layout_marginBottom="8dp" android:layout_marginEnd="4dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toLeftOf="@+id/button_share" app:layout_constraintHorizontal_chainStyle="spread" /> <Button android:id="@+id/button_share" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/button_share_text" android:layout_marginStart="4dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" app:layout_constraintLeft_toRightOf="@+id/button_save" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </android.support.constraint.ConstraintLayout> 

细节:

  • 设置每个项目的宽度为0dpMATCH_CONSTRAINT让视图填充父项(可选)
  • 视图必须双向链接(保存button链接到共享button的右侧,共享button的左侧链接到保存button),当select“中心水平”时,这将通过布局编辑器自动发生
  • 链中的第一个视图可以通过layout_constraintHorizontal_chainStyle指定链式样,查看各种链式样的文档 ,如果链式样被省略,默认是“展开”
  • 链的权重可以通过layout_constraintHorizontal_weight进行调整
  • 这个例子是一个水平链,垂直链有相应的属性

方法2 – 使用指南

在编辑器中打开布局,然后单击指南button:

在这里输入图像说明

然后select“添加垂直指南”: 在这里输入图像说明

将出现一个新的指导方针,默认情况下,可能会被锚定在相对值的左侧(用左箭头表示):

布局编辑器相关指南

点击向左的箭头将其切换到百分比值,然后将指南拖动到50%标记:

布局编辑百分比指南

该指南现在可以用作其他视图的锚点。 在我的示例中,我将保存button的右侧和分享button的左侧附加到指南:

最终布局

如果你想要视图填充可用的空间,那么约束应该设置为“任意大小”(水平运行的波浪线):

任何大小的限制

(这与将layout_width设置为0dp )。

也可以使用XML创build指南,而不是使用布局编辑器:

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

那么如果它有助于某人

关键是在这里的app:layout_constraintHorizontal_weight="1"
关于约束布局的最好的事情就是它支持循环依赖,这里就是我所做的。

对于第一个孩子
app:layout_constraintEnd_toStartOf="@+id/textInputSecondChild"

对于第二个孩子

app:layout_constraintLeft_toRightOf="@+id/textInputFirstChild"

这里是完整的演示

 <android.support.design.widget.TextInputLayout android:id="@+id/textInputParent" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"> <EditText android:id="@+id/editTextParent" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/state" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/textInputFirstChild" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toStartOf="@+id/textInputSecondChild" app:layout_constraintHorizontal_weight="1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/textInputParent"> <EditText android:id="@+id/editTextChildOne" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/pin_code" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/textInputSecondChild" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintHorizontal_weight="1" app:layout_constraintLeft_toRightOf="@+id/textInputFirstChild" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/textInputParent"> <EditText android:id="@+id/editTextChildSecond" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/country" /> </android.support.design.widget.TextInputLayout>