标签(空格分隔): Android
ConstraintLayout类似于RelativeLayout,主要用来做界面的操作。主要通过拖动来实现布局,基本上可以实现一层布局,提高性能。
提供了layout_constraintHorizontal_weight功能,补充了RelativeLayout不足。
没有match_parent,要使用0dp才行。
layout_constraint[当前控件被约束属性]_[参照控件属性]="[参照控件]"
比如
app:layout_constraintLeft_toLeftOf="parent"
代表当前控件的左边与父控件的左边对齐。
app:layout_constraintLeft_toRightOf="@id/cancel_button"
本控件的左边界与ID为cancel_button的右边界对齐
app:layout_constraintHorizontal_bias="0.25" 本控件在X轴1/4的地方开始
app:layout_constraintVertical_bias="0.25" 本控件在Y轴1/4的地方开始
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
倾向(Bias)
搭配bias,能使约束偏向某一边,默认是0.5,有以下属性:
- layout_constraintHorizontal_bias (0最左边 1最右边)
- layout_constraintVertical_bias (0最上边 1 最底边)
加入app:layout_constraintHorizontal_bias="0.9" ,则会在水平方向上向右偏移至90%。
android:layout_marginRight="10dp"
app:layout_goneMarginRight="110dp"
假如button1在button2右边,如果只有layout_marginRight,如果button1隐藏,button2会向右移动,margin parent为10dp,但是原来是110dp(假设button1宽度100dp),这时候就要设置layout_goneMarginRight,设置大小为layout_marginRight加上button1的宽度,layout_goneMarginRight在button1非gone状态时无作用。
app:layout_constraintDimensionRatio="4:3"
设置view的宽高比
app:layout_constraintDimensionRatio="W, 1:4"
当宽和高都为match_constraint即0dp时,约束方向为x轴时(宽为parent):
w, 1:2 高比宽; h, 2:1 宽比高
约束方向为y轴时(高为parent):
w, 1:2 宽比高; h, 2:1 高比宽
app:layout_constraintBaseline_toBaselineOf
基线对齐,放到控件上面,然后在控件中间偏下出现一道横线,拖动与另外一个控件的这道横线平齐。点击ab可以出现这道线,×可以删除这种约束。