约束类型:
- Relative positioning(相对定位)
<Button android:id="@+id/buttonA" ... />
<Button android:id="@+id/buttonB" ...
app:layout_constraintLeft_toRightOf="@+id/buttonA" />
可在横/竖轴方向添加相对约束
- 横轴: left, right, start and end sides
- 竖轴: top, bottom sides and text baseline
X和Y必须在同一轴向
<Button android:id="@+id/buttonB" ...
app:layout_constraintLeft_toLeftOf="parent" />
- Margins
- 通用间距
- android:layout_marginStart
- android:layout_marginXXX
- Gone间距,指定约束目标可见性为Gone时的间距
- layout_goneMarginStart
- layout_goneMarginXXX
- Centering positioning(居中定位)
1.约束居中
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
2.Bias(倾向居中)
- layout_constraintHorizontal_bias
- layout_constraintVertical_bias
<android.support.constraint.ConstraintLayout ...>
<Button android:id="@+id/button" ...
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent/>
</>
- Circular positioning(圆弧定位)
- layout_constraintCircle : references another widget id
- layout_constraintCircleRadius : the distance to the other
widget center -
layout_constraintCircleAngle : which angle the widget should be at (in degrees, from 0 to 360)
<Button android:id="@+id/buttonA" ... />
<Button android:id="@+id/buttonB" ...
app:layout_constraintCircle="@+id/buttonA"
app:layout_constraintCircleRadius="100dp"
app:layout_constraintCircleAngle="45" />
- Dimension constraints(尺寸约束)
1.android:layout_width和android:layout_height有三种设置方式:
- Using a specific dimension (either a literal value such as 123dp or a Dimension reference)
- Using WRAP_CONTENT, which will ask the widget to compute its own size
- Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"
android:minWidth设置WRAP_CONTENT模式下最大/最小尺寸
layout_constraintWidth_min设置MATCH_CONSTRAINT模式下最大/最小尺寸
2.Percent dimension
使用百分比大小需要以下步骤:
- 设置大小属性为MATCH_CONSTRAINT (0dp)
- 指定percent方式app:layout_constraintWidth_default="percent" or app:layout_constraintHeight_default="percent"
3.Ratio(长宽比例)
app:layout_constraintDimensionRatio="width:height"
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />
显示指定已知的一边"H,16:9"
<Button android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
- Chains(约束链)
约束链提供单轴向内的分组行为
在链头(最左或最上节点)设置layout_constraintHorizontal_chainStyle控制链组样式
- CHAIN_SPREAD(均分) -- the elements will be spread out (default style)
- Weighted chain -- in CHAIN_SPREAD mode, if some widgets are set to MATCH_CONSTRAINT, they will split the available space
- CHAIN_SPREAD_INSIDE(中间均分) -- similar, but the endpoints of the chain will not be spread out
- CHAIN_PACKED -- the elements of the chain will be packed together. The horizontal or vertical bias attribute of the child will then affect the positioning of the packed elements
- Virtual Helpers objects
用来辅助定位,在显示时不可见
- Guideline辅助线
- 指定到上/左的距离(layout_constraintGuide_begin)
- 指定到下/右的距离(layout_constraintGuide_end)
- 指定到上/左的距离百分比(layout_constraintGuide_percent)
辅助线的layout_width、layout_height无意义,layout_constraintGuide_begin和layout_constraintGuide_end同时存在以后者为准
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
app:layout_constraintGuide_begin="100dp"
android:orientation="vertical"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/guideline"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" />
- Barrier(栅栏)
<android.support.constraint.ConstraintLayout...>
<TextView
android:id=”@+id/text1" ... />
<TextView
android:id=”@+id/text2" ... />
<android.support.constraint.Barrier
android:id=”@+id/barrier”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
app:barrierDirection=”end” <!-- start, top, bottom, right... -->
app:constraint_referenced_ids=”text1,text2" />
<TextView
android:id=”@+id/text3"
...
app:layout_constraintStart_toEndOf=”@+id/barrier” />
</android.support.constraint.ConstraintLayout>
- Group
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="button4,button9" />
Group的可见性会被应用到app:constraint_referenced_ids属性链接的所有元素
- Placeholder(占位符)
Placeholder顾名思义,就是用来一个占位的东西,它可以把自己的内容设置为ConstraintLayout内的其它view。因此它用来写布局的模版,也可以用来动态修改UI的内容