android studio升级到stable 2.2之后,发现还有了个ConstraintLayout。看名字就是约束布局,用各种约束来确定widget的展示。该ConstraintLayout最低支持API 9。就是说老手机2.3以下是不支持的。
首先来看下一些主要的属性:
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
重要概念
ConstraintLayout约束布局的含义: 根据布局中的其他元素或视图, 确定View在屏幕中的位置. 包含三个重要信息, 根据其他视图设置位置, 根据父容器设置位置, 根据基准线设置位置.
layout_constraint[本源]_[目标]="[目标ID]"
使用
可以在 基线约束 与 基点约束 之间进行切换。
需要注意的是,任何在ConstraintLayout中的子控件,都要给控件加上constraint,如果不加,则默认绘制在constraintLayout坐标(0,0)的地方。
目前AndroidStudio的布局编辑器已经和好的支持ConstraintLayout了,可以轻松的在控件的某个边缘点击拖动鼠标,关联到另一个控件的某个边缘。十分方便,不需要依赖xml布局的编写。需要注意的是,当你用鼠标随意拖动控件的时候,控件会停留在鼠标离开的位置,即使这个控件没有任何的约束。所以该控件可能停留在布局的任何一个地方,但是刚刚说过了,一个控件在布局上无任何的约束,会画在ConstraintLayout坐标(0,0)的位置,所以preview中展示的样子,不一定是真实运行起来的样子!!!!!
根据Google的设计,下一步重要的布局方法就是ConstraintLayout了。
比较详细的解释参照: