12种排布方式
每个组件有4条边(上下左右)加上start (开始)和end(结束) 共计6个边沿,可以写出12种排布方式。
语句的格式如下:
<View A
app: layoutConstraintXXXtoYYYOf=”View B”>
View A 的XXX方向置于ViewB 的YYY方向上
12 个排布可以分为如下的3组:
1.left and right组
layoutConstraintLefttoLeftOf View A和View B 左对齐
layoutConstraintLefttoRightOf View A的左边置于viewB的右边
layoutConstraintRighttoRightOf 右对齐
layoutConstraintRighttoLeftOf View A的右边置于viewB 的左边
2.Top and Bottom组
layoutConstraintToptoTopOf 顶部对齐
layoutConstraintToptoBottomOf A 的上边置于B 的下边
layoutConstraintBottomtoBottomOf 底部对齐
layoutConstraintBottomtoTopOf A 的下边置于B 的上边
3.Start and End 组
只需把1组的left换成start ,right 换成end。
bias 偏差的计算
有两个方向的偏差 分别是:竖直方向 layout_constraintVertical_bias上间隙/上间隙+下间隙 约束顶边和底边时两个连接之间的比率
水平方向 layout_constraintHorizontal_bias左间隙/左间隙+右间隙 官方文档:当左右(或开始和结束)边受到约束时,两个连接之间的比率。
权重(weight)的计算
有两个方向的权重 ,分别是:
layout_constraintHorizontal_weight 和layout_constraintVertical_weight,
和线性布局类似都是剩余空间的占比但只对(0dp)match constraints 的组件生效。
Ratio 相关的问题 将尺寸设置为比率
视图尺寸设置为“match constraints”(0dp),您可以将视图尺寸设置为 尺寸比。如需启用该比率,请点击Toggle Aspect Ratio Constraint (左上角小三角 ),然后在 出现的输入框中输入width:height比率。
如果宽度和高度都设置为“match constraints。 您可以点击 Toggle Aspect Ratio Constraint,选 择哪个维度基于与另一个维度的比率。如果您将两侧都设置为"match constraints请双击Toggle Aspect Ratio Constraint,将宽度设置为与高度的比率。现在整个尺寸由视图的高度决定如图所示。