引言
去年在 Google I/O 大会上Google上发布了 ConstraintLayout,使得我们在构建复杂布局的同时能够让视图层级得到精简,而且可以通过布局工具拖拽轻松实现布局,不过这必须得Android Studio 2.2以后才有这个工具,但ConstraintLayout这个类本身是兼容到Android 2.3 的,最新版的Android Studio2.3.x版本已经默认把ConstraintLayout作为布局的根节点了,所以有必要总结下。
一、ConstraintLayout概述
ConstraintLayout约束布局和其他布局容器一样,都是继承自ViewGroup的,所以他也拥有其他布局的一些公用属性,与其他布局不同的是他是通过约束规则来实现布局的,所以他还新增了一些他特有的属性(后面再详说),虽然是在Android Studio2.2之后才有这个工具,但是向下兼容到Android版本2.3,官网中对它的描述就一句话:它允许您以灵活的方式定位和设置小部件。主要体现在可以以拖拽的方式来定义约束规则从而实现复杂的布局,当然你也可以通过原始的方式自己设置对应的属性。
二、ConstraintLayout所支持的约束类型
ConstraintLayout所支持的约束类型目前一共支持7中类型的约束:相对定位Relative positioning
外边距Margins、中心定位Centering positioning、可见性Visibility behavior、具体尺寸约束Dimension constraints、链式约束Chains、虚拟助手对象Virtual Helpers objects,值得注意的是目前还未支持在约束中具有循环依赖关系,设置布局有两种方式可以手拖拽也可以在XML文件中书写,本质都是一样的,手拖拽的方式只不过是自动生成对应的属性。
1、相对定位Relative positioning和中心定位Centering positioning
相对定位Relative positioning和中心定位Centering positioning是将给定的子View相对于另一个子View在水平和垂直轴上约束,其实和RelativeLayout差不多。简单的理解为把其中一个子View当成参照物,另一个参照它布局,对应的属性。
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
2、外边距Margins和可见性Visibility behavior
在常见的布局中如果一个子View可见状态为GONE时,设置外边距的时候就自动忽略掉了,而在ConstraintLayout中,除了共有的外边距属性之后还多了特有的 属性支持GONE状态的子View。
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom
3、具体尺寸约束Dimension constraints
4、Bias用于控制控件在水平和垂直方向在屏幕上的偏移比例
layout_constraintHorizontal_bias
layout_constraintVertical_bias
当为目标子View设置好横纵向的约束时(app:layout_constraintLeft_toLeftOf=”parent”、app:layout_constraintRight_toRightOf=”parent”或者app:layout_constraintTop_toTopOf=”parent”、app:layout_constraintBottom_toBottomOf=”parent”),这个两个属性才会生效。实际操作过程中,你会发现对着设置好横纵向约束的Button进行拖动,布局中的layout_constraintHorizontal_bias和layout_constraintVertical_bias会一直发生相应的变化,如果你需要Button居中,那么直接将这两个属性的参数值设置为0.5即可。
5、layout_constraintDimentionRatio设置子View的宽和高按某个比例进行布局
值得注意的是这个属性layout_constraintDimentionRatio生效的条件必须满足:目标子View的layout_width和layout_height至少有一个设置为0dp,layout_constraintDimentionRatio默认参数比例是指宽:高;而变成高:宽可以设app:layout_constraintDimensionRatio=”H,2:1”,例如 app:layout_constraintDimensionRatio=”H,3:1”
6、链式约束Chains
链式约束主要是用于设置一组子View的布局,当某一个子View设置了
layout_constraintHorizontal_chainStyle 或者layout_constraintVertical_chainStyle 属性(水平垂直方向的链式约束)
其中Weighted Chain链的默认是在可用空间中平均分配元素。如果一个或多个元素使用MATCH_CONSTRAINT,它们将使用可用的空白空间(在它们之间平分)。属性layout_constraintHorizontal_weight和layout_constraintVertical_weight将控制如何使用MATCH_CONSTRAINT在元素之间分配空间。例如,在使用MATCH_CONSTRAINT的包含两个元素的链上,第一个元素使用权重为2,第二个元素的权重为1,第一个元素占用的空间将是第二个元素的两倍。例如要把A、B、C按钮水平排成一行,可以用链式约束
这样ButtonA、B、C就在水平方向形成了一条Chain,并且底部对齐。此时ButtonA新增app:layout_constraintHorizontal_chainStyle的属性设置,这个属性在一条Chain中只会出现在第一个控件中,这个控件是整条Chain的Head
7、虚拟助手对象
在ConstraintLayout中有一类对象,在运行的时候不显示任何UI效果,只是作为参照物辅助布局,GuideLine就是其中之一。GuideLine分为水平引导线和垂直引导线。
并且支持设置在屏幕中所处的位置,可以使用layout_constraintGuide_begin和layout_constraintGuide_end设置具体dp值,也可以使用layout_constraintGuide_percent来设置比例。实际上它也只是一个辅助我们布局的View而已,其源码内部实现也非常简单,并且默认设置了visibility为gone((引自http://www.javashuo.com/article/p-utkjqwik-bnx.html))
三、ConstraintLayout的图形界面操作
1、约束手柄类型
主要有三种手柄类型:调整手柄、约束手柄、基线手柄
2、建立约束