標簽(空格分隔): Android
ConstraintLayout類似于RelativeLayout,主要用來做界面的操作。主要通過拖動來實現布局,基本上可以實現一層布局,提高性能。
提供了layout_constraintHorizontal_weight功能,補充了RelativeLayout不足。
沒有match_parent,要使用0dp才行。
layout_constraint[當前控件被約束屬性]_[參照控件屬性]="[參照控件]"
比如
app:layout_constraintLeft_toLeftOf="parent"
代表當前控件的左邊與父控件的左邊對齊。
app:layout_constraintLeft_toRightOf="@id/cancel_button"
本控件的左邊界與ID為cancel_button的右邊界對齊
app:layout_constraintHorizontal_bias="0.25" 本控件在X軸1/4的地方開始
app:layout_constraintVertical_bias="0.25" 本控件在Y軸1/4的地方開始
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
傾向(Bias)
搭配bias,能使約束偏向某一邊,默認是0.5,有以下屬性:
- layout_constraintHorizontal_bias (0最左邊 1最右邊)
- layout_constraintVertical_bias (0最上邊 1 最底邊)
加入app:layout_constraintHorizontal_bias="0.9" ,則會在水平方向上向右偏移至90%。
android:layout_marginRight="10dp"
app:layout_goneMarginRight="110dp"
假如button1在button2右邊,如果只有layout_marginRight,如果button1隱藏,button2會向右移動,margin parent為10dp,但是原來是110dp(假設button1寬度100dp),這時候就要設置layout_goneMarginRight,設置大小為layout_marginRight加上button1的寬度,layout_goneMarginRight在button1非gone狀態時無作用。
app:layout_constraintDimensionRatio="4:3"
設置view的寬高比
app:layout_constraintDimensionRatio="W, 1:4"
當寬和高都為match_constraint即0dp時,約束方向為x軸時(寬為parent):
w, 1:2 高比寬; h, 2:1 寬比高
約束方向為y軸時(高為parent):
w, 1:2 寬比高; h, 2:1 高比寬
app:layout_constraintBaseline_toBaselineOf
基線對齊,放到控件上面,然后在控件中間偏下出現一道橫線,拖動與另外一個控件的這道橫線平齊。點擊ab可以出現這道線,×可以刪除這種約束。