
繼承至ViewGroup
第一步:下載最新版的Android Studio
第二步:添加依賴
compile 'com.android.support.constraint:constraint-layout:1.0.0'
概念:
ConstraintLayout約束布局的含義: 根據(jù)布局中的其他元素或視圖, 確定View在屏幕中的位置. 包含三個(gè)重要信息, 根據(jù)其他視圖設(shè)置位置, 根據(jù)父容器設(shè)置位置, 根據(jù)基準(zhǔn)線設(shè)置位置.
使用
layout_constraint[本源]_[目標(biāo)]="[目標(biāo)ID]"
約束列表
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintLeft_toRightOf
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
app:layout_constraintBaseline_toBaselineOf? 表示此控件與某個(gè)控件水平對齊
舉例

layout_constraintTop_toTopOf
約束當(dāng)前Button的底部至目標(biāo)id為lay_root(代表我的父布局)的底部, 目標(biāo)View是ConstraintLayout. 表明, 把當(dāng)前View放置到lay_root(父容器)的底部, 并且底部一致.

layout_marginLeft
如果想使用它,必須設(shè)置layout_constraintLeft_toLeftOf(必須對應(yīng),如何設(shè)置右,必須設(shè)置toRight),否則將不能實(shí)現(xiàn)margin的設(shè)置
layout_constraintLeft_toRightOf? 設(shè)置按鈕2相對于按鈕1的位置。意思是:按鈕2左邊在按鈕1的右邊'
Margins邊距
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
比例 ---Bias
layout_constraintHorizontal_bias? 水平比例?
layout_constraintVertical_bias? ? 垂直比例
ConstraintLayout布局除了對齊屬性, 還有重要的比例屬性. 中心(Center)按鈕需要把全部邊界與constraintLayout(父容器)邊界對齊, 則為居中. 還可以設(shè)置layout_constraintHorizontal_bias和layout_constraintVertical_bias但必須設(shè)置約束(layout_constraintRight_toRightOf相對于父布局的right約束,如果不設(shè)置將無法使用比例)
app:layout_constraintVertical_bias="0.3"
視圖尺寸
ConstraintLayout布局中的控件也可以設(shè)置填充尺寸. 控件把寬度設(shè)置為0dp會自動根據(jù)位置進(jìn)行填充. 如button4按鈕, 左側(cè)對齊與button2按鈕的右側(cè), 右側(cè)對齊與lay_root父控件的右側(cè), 寬度設(shè)置為0dp, 實(shí)際會填充全部位置.

引導(dǎo)線
ConstraintLayout布局除了與布局對齊以外, 還可以與引導(dǎo)線(Guideline)對齊. 設(shè)置豎直引導(dǎo)線(Guideline)距離左側(cè)72dp. 按鈕(Button4)的左側(cè)都與引導(dǎo)線對齊(個(gè)人認(rèn)為類似LinearLayout設(shè)置內(nèi)邊距一樣)
app:layout_constraintGuide_begin
表示在布局中引導(dǎo)線距頂部或左邊框的距離(如:20dp表示距頂部或者左邊框20dp)
app:layout_constraintGuide_end
表示在布局中引導(dǎo)線距底部的距離(如:10dp表示距頂部10dp)
app:layout_constraintGuide_percent
表示在整個(gè)布局中引導(dǎo)線距離左邊框的百分百(如:app:layout_constraintGuide_percent="0.5"表示距離左邊框50%的位置)

視圖縱橫比(高寬比)
ConstraintLayout布局還可以使用constraintDimensionRatio設(shè)置視圖的縱橫比, 則需要把寬(layout_width)或者高(layout_height)設(shè)置為0dp, 根據(jù)另一個(gè)屬性和比例, 計(jì)算當(dāng)前屬性
