Guide_percent,指定在父控件中的宽度或高度的百分比,如0.8,表示距离顶部或者左侧的80%的距离。
android:orientation
设置垂直或者是水平
Guideline
是隐藏的,不用我们进行多余的设置,虽然外面在预览面板可以死看到它的存在
例子:使一个按钮的长度占满屏幕一半
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guidelineBegin"
app:layout_constraintGuide_percent="0.5"
android:orientation="vertical"/>
<Button
android:text="Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guidelineBegin"
app:layout_constraintTop_toTopOf="parent" />
Radio 比例 属性
方便快捷调整控件的宽高比,结合Guideline使用更佳
例子:
app:layout_constraintDimensionRatio
想要宽度与总布局一样,但高度是宽度的三分之一
宽高比为3:1
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:1"/>
Chain 链 属性
可以把这个当做成一个加强版的LinearLayout
由上图,很好的知道了A与B的约束,A的左边是父控件的左边,右边则是B,B的左边是A,B的右边的是父控件的右边
chainstyle
有三种属性,分别是spread
,spread_inside
,pack
,效果如下图
剩下的两种则是通过添加属性实现的
weighted chain
是在spread chain
的基础上,而packed chain with bias
则是在weight chain
的基础上
style为spread
的,使用layout_constraintHorizontal_weight
或layout_constraintVertical_weight
来设置weigh
权重
style为pack
的,使用layout_constraintHorizontal_bias
或layout_constraintVertical_bias
进行偏移设置,属性值0-1,也是百分比,改第一个元素
这里需要说明的是,除了水平,还可以是垂直排列,不过,不能通过属性来更改,而是通过约束来更改,把左边改为顶端,右边改为底部
如果是水平的,使用属性layout_constraintHorizontal_chainStyle
进行chainstyle属性的更改
垂直的则是使用layout_constraintVertical_chainStyle
例子:
三个按钮平分宽度(只需要将宽度设置为0dp
就可以达到目的)
<Button
android:id="@+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toStartOf="@id/btn1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintStart_toEndOf="@id/btn"
app:layout_constraintEnd_toStartOf="@id/btn2"/>
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintStart_toEndOf="@id/btn1"
app:layout_constraintEnd_toEndOf="parent"/>