基礎(chǔ)知識
1. Relative positioning相對定位
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_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
總結(jié):layout_constraintA_toB 參數(shù)為目標(biāo)id或者parent
A當(dāng)前控件磁玉,B目標(biāo)控件,A控件的哪一側(cè)(上下左右)與B控件的哪一側(cè)(上下左右)對齊
例如layout_constraintLeft_toLeftOf
控件A的左側(cè)和控件B的左側(cè)對齊
例如layout_constraintTop_toBottomOf
控件A的頂部和控件B的底部側(cè)對齊
注意薯鼠,如果當(dāng)前控件的layout_height
或者layout_width
值為match_parent
觉吭,約束會失效寡夹,改為0dp
即可
2. Margins
【1】基礎(chǔ)信息
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
注意,值必須為正數(shù)或者0彼宠,使用Dimension
【2】當(dāng)被依賴控件隱藏View.GONE
時浴捆,下面的幾個屬性生效
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom
當(dāng)約束目標(biāo)控件可見性設(shè)為View.GONE
時,啟用該屬性烙心,代碼示例如下
正常顯示,圖1
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainBkg">
?
<TextView
android:id="@+id/view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="50dp"
android:layout_marginTop="100dp"
android:background="#fff"
android:gravity="center"
android:text="目標(biāo)控件"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
?
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:background="#123"
android:gravity="center"
android:text="當(dāng)前控件"
android:textColor="#fff"
app:layout_constraintStart_toEndOf="@id/view"
app:layout_constraintTop_toTopOf="@id/view"
app:layout_goneMarginStart="100dp"
app:layout_goneMarginTop="200dp" />
?
</androidx.constraintlayout.widget.ConstraintLayout>
隱藏目標(biāo)控件后android:visibility="gone"
3. Centering positioning中心定位和偏差
當(dāng)控件左側(cè)與父控件左側(cè)對齊膜廊,右側(cè)與父控件右側(cè)對齊時app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
,除非控件大小和父控件大小一致淫茵,否側(cè)會在約束方向上(此時為橫向)居中顯示爪瓜,同理縱向也是一樣
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="居中"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
偏移量
當(dāng)控件在父控件中居中顯示或某方向居中顯示時,使用bias屬性調(diào)整控件偏移位置
layout_constraintHorizontal_bias
layout_constraintVertical_bias
匙瘪,方向為從上到下铆铆,從左到右,如果layout_constraintHorizontal_bias
設(shè)為1丹喻,位于父控件最右側(cè)薄货,layout_constraintVertical_bias
設(shè)為1,位于控件最底部
取值范圍float驻啤,0-1菲驴,如果大于1會顯示不全,默認(rèn)0.5
如圖骑冗,目標(biāo)控件原本居中顯示赊瞬,設(shè)置偏移量后位置改變
4. Circular positioning圓形定位
您可以以一定角度和距離將一個小部件中心相對于另一個小部件中心進行約束先煎。
layout_constraintCircle
定位控件ID
layout_constraintCircleRadius
兩個控件中心點之間的距離
layout_constraintCircleAngle
角度,取值范圍0-360的整數(shù)
5. Visibility behavior可見性處理
與其他layout的區(qū)別在于
- 當(dāng)控件設(shè)為
GONE
時巧涧,被認(rèn)為尺寸為0薯蝎,可以理解為布局上一個點 - 若
GONE
的控件對其他控件有約束,約束保留并生效谤绳,當(dāng)margin
會清零
6. Dimension constraints尺寸約束
6.1 為ContraintLayout
這只最大最小寬高
當(dāng)ContraintLayout
寬高設(shè)為WRAP_CONTENT
時可以設(shè)置最大最小寬高
-
android:minWidth
占锯、android:minHeight
最小寬高 -
android:maxWidth
、android:maxHeight
最大寬高
6.2 設(shè)置控件寬高
設(shè)置android:layout_width
,android:layout_height
參數(shù):
- 明確設(shè)置大小缩筛,如1dp
- 設(shè)為
WRAP_CONTENT
消略,控件自己計算大小 - 設(shè)為
0dp
,根據(jù)約束條件顯示大小
注意:不建議在ConstarintLayout
中使用MATCH_PARENT
瞎抛,建議設(shè)為0dp
艺演,約束條件中控件使用parnet
6.3 強制約束
當(dāng)控件設(shè)為WRAP_CONTENT
時,添加約束尺寸是不生效的桐臊,想要生效胎撤,需要添加如下屬性
app:layout_constrainedWidth=”true|false”
app:layout_constrainedHeight=”true|false”
6.4 MATCH_CONSTRAINT
- 改變最大最小,可以設(shè)固定dp數(shù)字断凶,也可設(shè)為
wrap
(相當(dāng)于WRAP_CONTENT
)
layout_constraintWidth_min
伤提、layout_constraintHeight_min
、layout_constraintWidth_max
认烁、layout_constraintHeight_max
- 設(shè)置百分比模式
- 尺寸大小設(shè)為
0dp
- 設(shè)置百分比模式
app:layout_constraintWidth_default="percent"
或app:layout_constraintHeight_default="percent"
- 設(shè)置所占大兄啄小(0-1)
layout_constraintWidth_percent
或layout_constraintHeight_percent
- 尺寸大小設(shè)為
- 控件寬高按照比例計算大小
- 控件寬高至少一個為
0dp
- 設(shè)置屬性
app:layout_constraintDimensionRatio
屬性取值可以是寬度比高度的比例(float),也可以是固定格式 "width:height"砚著,也可以是前綴W或H次伶,指定一邊相對于另一邊的尺寸,如”H, 16:9”稽穆,高比寬為16:9
- 控件寬高至少一個為
7. Chains 鏈條
在同一個軸上(水平或垂直)冠王,兩個或兩個以上控件首尾雙向連接,則將視為鏈條舌镶。鏈條由鏈條頭(最左側(cè)或左上方View)設(shè)置的屬性控制
- 如果在連接上指定邊距柱彻,將被使用,如在最左側(cè)添加margin餐胀,則整個鏈條左側(cè)有了margin效果
- 通過設(shè)置
layout_constraintHorizontal_chainStyle
哟楷、layout_constraintVertical_chainStyle
屬性來控制鏈條樣式。取值如下-
spread
默認(rèn)值否灾,元素平均散開展示,兩端顯示邊距 - 權(quán)重鏈條卖擅,在
spread
模式下,如果控件設(shè)為MATCH_CONSTRAINT
,將會占用可用空間惩阶,設(shè)置layout_constraintHorizontal_weight
或layout_constraintVertical_weight
控制權(quán)重(類似LinearLayout
中weight
) -
spread_inside
類似spread
模式挎狸,兩端不會展示margin -
packed
控件壓縮在一起,兩端均勻展示margin断楷,通過設(shè)置鏈條頭偏移量可以控制鏈條整體偏移位置
鏈條樣式
-