ConstraintLayout分享
控制元件如何確定自己的位置,最簡單的基本操作就是:
layout_constraint[自己位置]_[目標位置]=目標ID
基本佈局屬性:
layout_constraintLeft_toLeftOf layout_constraintLeft_toRightOf layout_constraintRight_toLeftOf layout_constraintRight_toRightOf layout_constraintTop_toTopOf layout_constraintTop_toBottomOf layout_constraintBottom_toTopOf layout_constraintBottom_toBottomOf layout_constraintStart_toEndOf layout_constraintStart_toStartOf layout_constraintEnd_toStartOf layout_constraintEnd_toEndOf layout_constraintBaseline_toBaselineOf 複製程式碼
舉個例子:

可以看到B在A的右邊,我們可以這麼寫:
<Button android:id="@+id/buttonA" ... /> <Button android:id="@+id/buttonB" ... app:layout_constraintLeft_toRightOf="@+id/buttonA" /> 複製程式碼
可以理解B的左邊在A的右邊,這樣B就貼在A的右邊了。
我們發現上面還有一個 layout_constraintBaseline_toBaselineOf
,直接看下圖就可以理解所有相關的屬性:

如果是相對於父佈局,我們也可以不寫入另外一個控制元件的id值,直接填parent值就可以了
<android.support.constraint.ConstraintLayout ...> <Button android:id="@+id/button" ... app:layout_constraintLeft_toLeftOf="parent" /> <android.support.constraint.ConstraintLayout/> 複製程式碼
Margin相關

當A處於gone的時候,我們可以讓B的margin值是根據以下的屬性值:
layout_goneMarginStart layout_goneMarginEnd layout_goneMarginLeft layout_goneMarginTop layout_goneMarginRight layout_goneMarginBottom 複製程式碼
水平和垂直方向所佔比例
layout_constraintHorizontal_bias layout_constraintVertical_bias 複製程式碼
- 水平居中
<android.support.constraint.ConstraintLayout ...> <Button android:id="@+id/button" ... app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent/> <android.support.constraint.ConstraintLayout/> 複製程式碼
- 水平方向佔比
<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/> </android.support.constraint.ConstraintLayout> 複製程式碼
圓形佈局

例如:
<Button android:id="@+id/buttonA" ... /> <Button android:id="@+id/buttonB" ... app:layout_constraintCircle="@+id/buttonA" app:layout_constraintCircleRadius="100dp" app:layout_constraintCircleAngle="45" /> 複製程式碼
尺寸限制
- 對ConstraintLayout進行限制
android:minWidth設定佈局的最小寬度 android:minHeight設定佈局的最小高度 android:maxWidth設定佈局的最大寬度 android:maxHeight設定佈局的最大高度 複製程式碼
- 對內部的控制元件進行限制
// WRAP_CONTENT app:layout_constrainedWidth ="true|false" app:layout_constrainedHeight ="true|false" // MATCH_CONSTRAINT(也就是0dp) layout_constraintWidth_min和layout_constraintHeight_min:將設定此維度的最小尺寸 layout_constraintWidth_max和layout_constraintHeight_max:將設定此維度的最大尺寸 layout_constraintWidth_percent和layout_constraintHeight_percent:將設定此維度的大小為父級的百分比 複製程式碼
- 寬高比
app:layout_constraintDimensionRatio="W|H,1:1" 複製程式碼
鏈

layout_constraintHorizontal_chainStyle layout_constraintVertical_chainStyle 複製程式碼

其他輔助控制元件
- Guideline
- Barrier
- Group
- Placeholder 這些在分享的時候在演示QAQ