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>

左右排列更嚴謹?shù)膶懛?/p>
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"
上面就是構成官網(wǎng)所說的鏈(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以此排列

源碼
相關鏈接
ConstraintLayout詳解
Android新特性介紹峦甩,ConstraintLayout完全解析
Constraint Layout 1.1.x帶來了哪些新東西嘱朽?
未來布局之星——ConstraintLayout
ConstraintLayout使用的一些坑
Android約束布局ConstraintLayout 項目實戰(zhàn)攻略
Android ConstraintLayout詳解
實戰(zhàn)篇ConstraintLayout的崛起之路
關于ConstraintLayout與Recycleview使用中的一些坑
ConstraintLayout使用的一些坑