約束類型:
- Relative positioning(相對(duì)定位)
B在A的右邊
<Button android:id="@+id/buttonA" ... />
<Button android:id="@+id/buttonB" ...
app:layout_constraintLeft_toRightOf="@+id/buttonA" />
可在橫/豎軸方向添加相對(duì)約束
- 橫軸: left, right, start and end sides
- 豎軸: top, bottom sides and text baseline
相對(duì)位置約束示
X和Y必須在同一軸向
<Button android:id="@+id/buttonB" ...
app:layout_constraintLeft_toLeftOf="parent" />
- Margins
- 通用間距
- android:layout_marginStart
- android:layout_marginXXX
- Gone間距缰雇,指定約束目標(biāo)可見性為Gone時(shí)的間距
- layout_goneMarginStart
- layout_goneMarginXXX
- Centering positioning(居中定位)
1.約束居中
設(shè)置具體大小或者wrap_content
<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" />
設(shè)置0dp或者match_constraint
<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
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有三種設(shè)置方式:
- 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設(shè)置WRAP_CONTENT模式下最大/最小尺寸
layout_constraintWidth_min設(shè)置MATCH_CONSTRAINT模式下最大/最小尺寸
2.Percent dimension
使用百分比大小需要以下步驟:
- 設(shè)置大小屬性為MATCH_CONSTRAINT (0dp)
- 指定percent方式app:layout_constraintWidth_default="percent" or app:layout_constraintHeight_default="percent"
3.Ratio(長(zhǎng)寬比例)
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(約束鏈)
約束鏈提供單軸向內(nèi)的分組行為
約束鏈以及鏈頭
在鏈頭(最左或最上節(jié)點(diǎn))設(shè)置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
用來(lái)輔助定位巩螃,在顯示時(shí)不可見
- Guideline輔助線
- 指定到上/左的距離(layout_constraintGuide_begin)
- 指定到下/右的距離(layout_constraintGuide_end)
- 指定到上/左的距離百分比(layout_constraintGuide_percent)
輔助線的layout_width、layout_height無(wú)意義,layout_constraintGuide_begin和layout_constraintGuide_end同時(shí)存在以后者為準(zhǔn)
Guideline
<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(柵欄)
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的可見性會(huì)被應(yīng)用到app:constraint_referenced_ids屬性鏈接的所有元素