1.介紹
2.為什么要用ConstraintLayout
3.如何使用ConstraintLayout
3.1 添加依賴
3.2 相對(duì)定位
3.3 角度定位
3.4 邊距
3.5 居中和偏移
3.6 尺寸約束
3.7 鏈
4.輔助工具
4.1 Optimizer
4.2 Barrier
4.3 Group
4.4 Placeholder
4.5.Guideline
5.總結(jié)
1.介紹
約束布局ConstraintLayout是一個(gè)ViewGroup,可以在Api9以上的Android系統(tǒng)使用它,它的出現(xiàn)主要是為了解決布局嵌套過多的問題,以靈活的方式定位和調(diào)整小部件。從Android Studio 2.3起,官方的模板默認(rèn)使用ConstraintLayout。
2.為什么要用ConstraintLayout
在開發(fā)過程中經(jīng)常能遇到一些復(fù)雜的UI,可能會(huì)出現(xiàn)布局嵌套過多的問題,嵌套得越多,設(shè)備繪制視圖所需的時(shí)間和計(jì)算功耗也就越多。簡(jiǎn)單舉個(gè)例子:

假設(shè)現(xiàn)在要寫一個(gè)這樣的布局,可能有人會(huì)這么寫:
首先是一個(gè)垂直的LinearLayout,里面放兩個(gè)水平的LinearLayout,然后在水平的LinearLayout里面放TextView。這樣的寫法就嵌套了兩層LinearLayout。

有些人考慮到了嵌套布局帶來的風(fēng)險(xiǎn),所以用一個(gè)RelativeLayout來裝下所有的控件。那么問題來了,既然用RelativeLayout可以解決問題,為什么還要使用ConstraintLayout呢?因?yàn)镃onstraintLayout使用起來比RelativeLayout更靈活,性能更出色!還有一點(diǎn)就是ConstraintLayout可以按照比例約束控件位置和尺寸,能夠更好地適配屏幕大小不同的機(jī)型。
3.如何使用ConstraintLayout
3.1 添加依賴
首先我們需要在app/build.gradle文件中添加ConstraintLayout的依賴,如下所示。
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
3.2 相對(duì)定位
相對(duì)定位是部件對(duì)于另一個(gè)位置的約束,這么說可能有點(diǎn)抽象,舉個(gè)例子:

如圖所示,TextView2在TextView1的右邊,TextView3在TextView1的下面,這個(gè)時(shí)候在布局文件里面應(yīng)該這樣寫:
<TextView? ? ? ? android:id="@+id/TextView1"...android:text="TextView1"/><TextView? ? ? ? android:id="@+id/TextView2"...app:layout_constraintLeft_toRightOf="@+id/TextView1"/><TextView? ? ? ? android:id="@+id/TextView3"...app:layout_constraintTop_toBottomOf="@+id/TextView1"/>
上面代碼中在TextView2里用到了app:layout_constraintLeft_toRightOf="@+id/TextView1"這個(gè)屬性,他的意思是把TextView2的左邊約束到TextView1的右邊,如下圖所示:

同理TextView3在TextView1的下面,就需要用到app:layout_constraintTop_toBottomOf="@+id/TextView1",即把TextView3的上面約束到TextView1的下面。
下面來看看相對(duì)定位的常用屬性:
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
上面屬性中有一個(gè)比較有趣的layout_constraintBaseline_toBaselineOf
Baseline指的是文本基線,舉個(gè)例子:

如圖所示,兩個(gè)TextView的高度不一致,但是又希望他們文本對(duì)齊,這個(gè)時(shí)候就可以使用layout_constraintBaseline_toBaselineOf,代碼如下:
<TextView? ? ? ? android:id="@+id/TextView1".../><TextView? ? ? ? android:id="@+id/TextView2"...app:layout_constraintLeft_toRightOf="@+id/TextView1"app:layout_constraintBaseline_toBaselineOf="@+id/TextView1"/>
效果如下:

ConstraintLayout相對(duì)定位的用法跟RelativeLayout還是比較相似的,下面用一個(gè)圖來總結(jié)相對(duì)定位:

3.3 角度定位
角度定位指的是可以用一個(gè)角度和一個(gè)距離來約束兩個(gè)空間的中心。舉個(gè)例子:
<TextView? ? ? ? android:id="@+id/TextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextView? ? ? ? android:id="@+id/TextView2"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintCircle="@+id/TextView1"app:layout_constraintCircleAngle="120"app:layout_constraintCircleRadius="150dp"/>
上面例子中的TextView2用到了3個(gè)屬性:
app:layout_constraintCircle="@+id/TextView1"
app:layout_constraintCircleAngle="120"(角度)
app:layout_constraintCircleRadius="150dp"(距離)
指的是TextView2的中心在TextView1的中心的120度,距離為150dp,效果如下:

3.4 邊距
3.4.1 常用margin
ConstraintLayout的邊距常用屬性如下:
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
看起來跟別的布局沒有什么差別,但實(shí)際上控件在ConstraintLayout里面要實(shí)現(xiàn)margin,必須先約束該控件在ConstraintLayout里的位置,舉個(gè)例子:
<android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/TextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"/></android.support.constraint.ConstraintLayout>
如果在別的布局里,TextView1的位置應(yīng)該是距離邊框的左邊和上面有一個(gè)10dp的邊距,但是在ConstraintLayout里,是不生效的,因?yàn)闆]有約束TextView1在布局里的位置。正確的寫法如下:
<android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/TextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"/></android.support.constraint.ConstraintLayout>
把TextView1的左邊和上邊約束到parent的左邊和上邊,這樣margin就會(huì)生效,效果如下:

在使用margin的時(shí)候要注意兩點(diǎn):
控件必須在布局里約束一個(gè)相對(duì)位置
margin只能大于等于0
3.4.2 goneMargin
goneMargin主要用于約束的控件可見性被設(shè)置為gone的時(shí)候使用的margin值,屬性如下:
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom
舉個(gè)例子:
假設(shè)TextView2的左邊約束在TextView1的右邊,并給TextView2設(shè)一個(gè)app:layout_goneMarginLeft="10dp",代碼如下:
<android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/TextView1".../><TextViewandroid:id="@+id/TextView2"...app:layout_constraintLeft_toRightOf="@+id/TextView1"app:layout_goneMarginLeft="10dp"/></android.support.constraint.ConstraintLayout>
效果如下,TextView2在TextView1的右邊,且沒有邊距。

這個(gè)時(shí)候把TextView1的可見性設(shè)為gone,效果如下:

TextView1消失后,TextView2有一個(gè)距離左邊10dp的邊距。
3.5 居中和偏移
在RelativeLayout中,把控件放在布局中間的方法是把layout_centerInParent設(shè)為true,而在ConstraintLayout中的寫法是:
app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"
意思是把控件的上下左右約束在布局的上下左右,這樣就能把控件放在布局的中間了。同理RelativeLayout中的水平居中l(wèi)ayout_centerHorizontal相當(dāng)于在ConstraintLayout約束控件的左右為parent的左右;RelativeLayout中的垂直居中l(wèi)ayout_centerVertical相當(dāng)于在ConstraintLayout約束控件的上下為parent的上下。
由于ConstraintLayout中的居中已經(jīng)為控件約束了一個(gè)相對(duì)位置,所以可以使用margin,如下所示:
<TextView? ? ? ? android:id="@+id/TextView1"...android:layout_marginLeft="100dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"/>
效果如下:

上面TextView1在水平居中后使用layout_marginLeft="100dp"向右偏移了100dp。除了這種偏移外,ConstraintLayout還提供了另外一種偏移的屬性:
layout_constraintHorizontal_bias? 水平偏移
layout_constraintVertical_bias? 垂直偏移
舉個(gè)例子:
<TextView? ? ? ? android:id="@+id/TextView1"...app:layout_constraintHorizontal_bias="0.3"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"/>
效果如下:

假如現(xiàn)在要實(shí)現(xiàn)水平偏移,給TextView1的layout_constraintHorizontal_bias賦一個(gè)范圍為 0-1 的值,假如賦值為0,則TextView1在布局的最左側(cè),假如賦值為1,則TextView1在布局的最右側(cè),假如假如賦值為0.5,則水平居中,假如假如賦值為0.3,則更傾向于左側(cè)。
垂直偏移同理。
3.6 尺寸約束
控件的尺寸可以通過四種不同方式指定:
使用指定的尺寸
使用wrap_content,讓控件自己計(jì)算大小
當(dāng)控件的高度或?qū)挾葹閣rap_content時(shí),可以使用下列屬性來控制最大、最小的高度或?qū)挾龋?/p>
android:minWidth 最小的寬度
android:minHeight 最小的高度
android:maxWidth 最大的寬度
android:maxHeight 最大的高度
注意!當(dāng)ConstraintLayout為1.1版本以下時(shí),使用這些屬性需要加上強(qiáng)制約束,如下所示:
app:constrainedWidth=”true”
app:constrainedHeight=”true”
使用 0dp (MATCH_CONSTRAINT)
官方不推薦在ConstraintLayout中使用match_parent,可以設(shè)置 0dp (MATCH_CONSTRAINT) 配合約束代替match_parent,舉個(gè)例子:
<TextView? ? ? ? android:id="@+id/TextView1"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginLeft="50dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"android:visibility="visible"/>
寬度設(shè)為0dp,左右兩邊約束parent的左右兩邊,并設(shè)置左邊邊距為50dp,效果如下:

寬高比
當(dāng)寬或高至少有一個(gè)尺寸被設(shè)置為0dp時(shí),可以通過屬性layout_constraintDimensionRatio設(shè)置寬高比,舉個(gè)例子:
<TextView? ? ? ? android:id="@+id/TextView1"android:layout_width="0dp"android:layout_height="wrap_content"app:layout_constraintDimensionRatio="1:1"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"/>
寬設(shè)置為0dp,寬高比設(shè)置為1:1,這個(gè)時(shí)候TextView1是一個(gè)正方形,效果如下:

除此之外,在設(shè)置寬高比的值的時(shí)候,還可以在前面加W或H,分別指定寬度或高度限制。 例如:
app:layout_constraintDimensionRatio="H,2:3"指的是? 高:寬=2:3
app:layout_constraintDimensionRatio="W,2:3"指的是? 寬:高=2:3
3.7 鏈
如果兩個(gè)或以上控件通過下圖的方式約束在一起,就可以認(rèn)為是他們是一條鏈(圖為橫向的鏈,縱向同理)。

用代碼表示:
<TextView? ? ? ? android:id="@+id/TextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@+id/TextView2"/><TextView? ? ? ? android:id="@+id/TextView2"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintLeft_toRightOf="@+id/TextView1"app:layout_constraintRight_toLeftOf="@+id/TextView3"app:layout_constraintRight_toRightOf="parent"/><TextView? ? ? ? android:id="@+id/TextView3"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintLeft_toRightOf="@+id/TextView2"app:layout_constraintRight_toRightOf="parent"/>
3個(gè)TextView相互約束,兩端兩個(gè)TextView分別與parent約束,成為一條鏈,效果如下:

一條鏈的第一個(gè)控件是這條鏈的鏈頭,我們可以在鏈頭中設(shè)置 layout_constraintHorizontal_chainStyle來改變整條鏈的樣式。chains提供了3種樣式,分別是:
CHAIN_SPREAD —— 展開元素 (默認(rèn));
CHAIN_SPREAD_INSIDE —— 展開元素,但鏈的兩端貼近parent;
CHAIN_PACKED —— 鏈的元素將被打包在一起。
如圖所示:

上面的例子創(chuàng)建了一個(gè)樣式鏈,除了樣式鏈外,還可以創(chuàng)建一個(gè)權(quán)重鏈。
可以留意到上面所用到的3個(gè)TextView寬度都為wrap_content,如果我們把寬度都設(shè)為0dp,這個(gè)時(shí)候可以在每個(gè)TextView中設(shè)置橫向權(quán)重layout_constraintHorizontal_weight(constraintVertical為縱向)來創(chuàng)建一個(gè)權(quán)重鏈,如下所示:
<TextView? ? ? ? android:id="@+id/TextView1"android:layout_width="0dp"android:layout_height="wrap_content"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toLeftOf="@+id/TextView2"app:layout_constraintHorizontal_weight="2"/><TextView? ? ? ? android:id="@+id/TextView2"android:layout_width="0dp"android:layout_height="wrap_content"app:layout_constraintLeft_toRightOf="@+id/TextView1"app:layout_constraintRight_toLeftOf="@+id/TextView3"app:layout_constraintRight_toRightOf="parent"app:layout_constraintHorizontal_weight="3"/><TextView? ? ? ? android:id="@+id/TextView3"android:layout_width="0dp"android:layout_height="wrap_content"app:layout_constraintLeft_toRightOf="@+id/TextView2"app:layout_constraintRight_toRightOf="parent"app:layout_constraintHorizontal_weight="4"/>
效果如下:

4.輔助工具
4.1 Optimizer
當(dāng)我們使用 MATCH_CONSTRAINT 時(shí),ConstraintLayout 將對(duì)控件進(jìn)行 2 次測(cè)量,ConstraintLayout在1.1中可以通過設(shè)置layout_optimizationLevel進(jìn)行優(yōu)化,可設(shè)置的值有:
none:無優(yōu)化
standard:僅優(yōu)化直接約束和屏障約束(默認(rèn))
direct:優(yōu)化直接約束
barrier:優(yōu)化屏障約束
chain:優(yōu)化鏈約束
dimensions:優(yōu)化尺寸測(cè)量
4.2 Barrier

假設(shè)有3個(gè)控件ABC,C在AB的右邊,但是AB的寬是不固定的,這個(gè)時(shí)候C無論約束在A的右邊或者B的右邊都不對(duì)。當(dāng)出現(xiàn)這種情況可以用Barrier來解決。Barrier可以在多個(gè)控件的一側(cè)建立一個(gè)屏障,如下所示:

這個(gè)時(shí)候C只要約束在Barrier的右邊就可以了,代碼如下:
<TextView? ? ? ? android:id="@+id/TextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextView? ? ? ? android:id="@+id/TextView2"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@+id/TextView1"/><android.support.constraint.Barrier? ? ? ? android:id="@+id/barrier"android:layout_width="wrap_content"android:layout_height="wrap_content"app:barrierDirection="right"app:constraint_referenced_ids="TextView1,TextView2"/><TextView? ? ? ? android:id="@+id/TextView3"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintLeft_toRightOf="@+id/barrier"/>
app:barrierDirection為屏障所在的位置,可設(shè)置的值有:bottom、end、left、right、start、top
app:constraint_referenced_ids為屏障引用的控件,可設(shè)置多個(gè)(用“,”隔開)
4.3 Group
Group可以把多個(gè)控件歸為一組,方便隱藏或顯示一組控件,舉個(gè)例子:
<TextView? ? ? ? android:id="@+id/TextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextView? ? ? ? android:id="@+id/TextView2"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintLeft_toRightOf="@+id/TextView1"/><TextView? ? ? ? android:id="@+id/TextView3"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintLeft_toRightOf="@id/TextView2"/>

現(xiàn)在有3個(gè)并排的TextView,用Group把TextView1和TextView3歸為一組,再設(shè)置這組控件的可見性,如下所示:
<android.support.constraint.Group? ? ? ? android:id="@+id/group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="invisible"app:constraint_referenced_ids="TextView1,TextView3"/>
效果如下:

4.4 Placeholder
Placeholder指的是占位符。在Placeholder中可使用setContent()設(shè)置另一個(gè)控件的id,使這個(gè)控件移動(dòng)到占位符的位置。舉個(gè)例子:
<android.support.constraint.Placeholder? ? ? ? android:id="@+id/placeholder"android:layout_width="wrap_content"android:layout_height="wrap_content"app:content="@+id/textview"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"/><TextView? ? ? ? android:id="@+id/textview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#cccccc"android:padding="16dp"android:text="TextView"android:textColor="#000000"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"/>
新建一個(gè)Placeholder約束在屏幕的左上角,新建一個(gè)TextView約束在屏幕的右上角,在Placeholder中設(shè)置 app:content="@+id/textview",這時(shí)TextView會(huì)跑到屏幕的左上角。效果如下:

4.5 Guideline
Guildline像輔助線一樣,在預(yù)覽的時(shí)候幫助你完成布局(不會(huì)顯示在界面上)。
Guildline的主要屬性:
android:orientation 垂直vertical,水平horizontal
layout_constraintGuide_begin 開始位置
layout_constraintGuide_end 結(jié)束位置
layout_constraintGuide_percent 距離頂部的百分比(orientation = horizontal時(shí)則為距離左邊)
舉個(gè)例子:
<android.support.constraint.Guideline? ? ? ? android:id="@+id/guideline1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"app:layout_constraintGuide_begin="50dp"/><android.support.constraint.Guideline? ? ? ? android:id="@+id/guideline2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"app:layout_constraintGuide_percent="0.5"/>
guideline1為水平輔助線,開始位置是距離頂部50dp,guideline2位垂直輔助線,開始位置為屏幕寬的0.5(中點(diǎn)位置),效果如下:

5.總結(jié)
本篇文章主要介紹了ConstraintLayout和它在布局文件里的用法,和一些輔助ConstraintLayout布局的工具,跟著敲一遍就能學(xué)會(huì)ConstraintLayout。除此之外,ConstraintLayout還有一個(gè)獨(dú)立的編輯器,只需要托拉拽就可以完成整個(gè)布局,但我個(gè)人比較喜歡直接用代碼寫,就不介紹了,有興趣的可以參考https://blog.csdn.net/guolin_blog/article/details/53122387