1.android佈局-ConstraintLayout-約束佈局
ConstraintLayout 是什麼?
ofollow,noindex">ConstraintLayout
ConstraintLayout 怎麼用?
基本用法
layout_constraint[當前控制元件位置]_[目標控制元件位置]="[目標控制元件ID]"
1.上下排列
a b b: app:layout_constraintTop_toBottomOf="a"
例項:
<android.support.constraint.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"> <TextView android:id="@+id/a" style="@style/text" android:text="@string/a" /> <TextView android:id="@+id/b" style="@style/text" android:text="@string/b" app:layout_constraintTop_toBottomOf="@id/a" /> </android.support.constraint.ConstraintLayout>
2.左右排列
a b a: app:layout_constraintRight_toLeftOf="b" b: app:layout_constraintLeft_toRightOf="a"
例項:
<android.support.constraint.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"> <TextView android:id="@+id/a" style="@style/text" app:layout_constraintRight_toLeftOf="@id/b" android:text="@string/a" /> <TextView android:id="@+id/b" style="@style/text" android:text="@string/b" app:layout_constraintLeft_toRightOf="@id/a" /> </android.support.constraint.ConstraintLayout>
左右排列更嚴謹的寫法
a b b: app:layout_constraintStart_toEndOf="a" app:layout_constraintTop_toTopOf="a" app:layout_constraintBottom_toBottomOf="a"
例項:
<android.support.constraint.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"> <TextView android:id="@+id/a" style="@style/text" app:layout_constraintRight_toLeftOf="@id/b" app:layout_constraintEnd_toStartOf="@id/b" app:layout_constraintTop_toTopOf="@id/b" android:text="@string/a" /> <TextView android:id="@+id/b" style="@style/text" android:text="@string/b" app:layout_constraintStart_toEndOf="@id/a" app:layout_constraintTop_toTopOf="@id/a" app:layout_constraintLeft_toRightOf="@id/a" /> </android.support.constraint.ConstraintLayout>
3.靠最右邊
a 靠介面最右邊 a: app:layout_constraiontEnd_toEndOf="parent"
例項:
<android.support.constraint.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"> <TextView android:id="@+id/a" style="@style/text" app:layout_constraintEnd_toEndOf="parent" android:text="@string/a" /> </android.support.constraint.ConstraintLayout>
constraiontEnd 是自己本身的 toEnd 底部停靠的 parent 是父佈局
4.三等分然後停靠最底部
a b c a: app:layout_constraiontBottom_toBottomOf="parent" app:layout_constraiontLeft_toLeftOf="parent" app:layout_constraiontRight_toLeftOf="b" b: app:layout_constraiontBottom_toBottomOf="parent" app:layout_constraiontLeft_toRightOf="a" app:layout_constraiontRight_toLeftOf="c" c: app:layout_constraiontBottom_toBottomOf="parent" app:layout_constraiontLeft_toRightOf="b" app:layout_constraiontRight_toRightOf="parent"
上面就是構成官網所說的鏈(chain)
例項:
<android.support.constraint.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"> <TextView android:id="@+id/a" style="@style/text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/b" android:text="@string/a" /> <TextView android:id="@+id/b" style="@style/text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toRightOf="@id/a" app:layout_constraintRight_toLeftOf="@id/c" android:text="@string/b"/> <TextView android:id="@+id/c" style="@style/text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toRightOf="@id/b" android:text="@string/c"/> </android.support.constraint.ConstraintLayout>
延伸兩個屬性
app:layout_constraiontHorizontal_weight="1" //等分屬性 app:layout_constraiontHorizontal_chainstyle="spread_inside" //spread_inside 兩端對齊 packed 聚中spread(預設)控制元件之間留空
例項:
聚中:
<android.support.constraint.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"> <TextView android:id="@+id/a" style="@style/text" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/b" android:text="@string/a" /> <TextView android:id="@+id/b" style="@style/text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toRightOf="@id/a" app:layout_constraintRight_toLeftOf="@id/c" android:text="@string/b"/> <TextView android:id="@+id/c" style="@style/text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toRightOf="@id/b" android:text="@string/c"/> </android.support.constraint.ConstraintLayout>
左右分散:
<android.support.constraint.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"> <TextView android:id="@+id/a" style="@style/text" app:layout_constraintHorizontal_chainStyle="spread_inside" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/b" android:text="@string/a" /> <TextView android:id="@+id/b" style="@style/text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toRightOf="@id/a" app:layout_constraintRight_toLeftOf="@id/c" android:text="@string/b"/> <TextView android:id="@+id/c" style="@style/text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toRightOf="@id/b" android:text="@string/c"/> </android.support.constraint.ConstraintLayout>
Guideline (約束線)
例項:
<android.support.constraint.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"> <android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_constraintGuide_percent="0.5" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/guideline" app:layout_constraintWidth_default="spread" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintBottom_toBottomOf="@+id/button" app:layout_constraintStart_toStartOf="@+id/guideline" app:layout_constraintTop_toTopOf="@+id/button" /> </android.support.constraint.ConstraintLayout> Guideline 是一個約束線(不會畫出來) app:layout_constraintGuide_percent="0.5" 0.5是50% 上面程式碼是 在檢視50%位置 左右兩邊有兩個butthon
例項:
<android.support.constraint.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"> <android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" app:layout_constraintGuide_begin="40dp"/> <TextView android:id="@+id/a" style="@style/text" app:layout_constraintTop_toTopOf="@id/guideline" android:text="@string/a"/> <TextView android:id="@+id/b" style="@style/text" app:layout_constraintTop_toTopOf="@id/guideline" app:layout_constraintLeft_toRightOf="@id/a" android:text="@string/b"/> <TextView android:id="@+id/c" style="@style/text" app:layout_constraintTop_toTopOf="@id/guideline" app:layout_constraintLeft_toRightOf="@id/b" android:text="@string/c"/> </android.support.constraint.ConstraintLayout> 在40dp上畫一條約束線 textview以此排列