ConstraintLayout是AndroidStudio2.2新增的一個功能,那么這個到底是什么呢?首先第一點我們知道傳統(tǒng)的安卓開發(fā),頁面基本都是XML編寫實現(xiàn),特別在一些復(fù)雜的頁面上需要嵌套多層,降低了頁面加載的效率,因為ConstraintLayout就可以很好的優(yōu)化布局,還有一點我們羨慕IOS的拖拽XML頁面在這里也可以更好的實現(xiàn)。當然我所說的以上兩點都是優(yōu)化以前的布局,這也是Google極力要做的事情
開始
想要使用ConstraintLayout,首先AS版本必須升級到2.2以上(我基本都是逢新必更),首先需要在app/build.gradle文件中添加ConstraintLayout依賴
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
然后在項目的build.gradle文件buildscript和allprojects的repositories中添加google()
allprojects {
repositories {
google()
jcenter()
}
}
然后同步就可以愉快的使用ConstraintLayout了~~
首先我們按照Google文檔的順序依次學(xué)習(xí)https://developer.android.com/reference/android/support/constraint/ConstraintLayout 先來一波api
1. layout_constraint[自身控件位置]_to[目標控件位置]Of=="[目標控件ID]"
layout_constraintLeft_toLeftOflayout_constraintLeft_toRightOflayout_constraintRight_toLeftOflayout_constraintRight_toRightOflayout_constraintTop_toTopOflayout_constraintTop_toBottomOflayout_constraintBottom_toTopOflayout_constraintBottom_toBottomOflayout_constraintBaseline_toBaselineOflayout_constraintStart_toEndOflayout_constraintStart_toStartOflayout_constraintEnd_toStartOflayout_constraintEnd_toEndOf
看到這些猜也能猜出個大概比如layout_constraintLeft_toLeftOf就是說當前控件的Left在目標控件的Left上。如果目標控件為父控件則id可以直接寫成parent。比如要實現(xiàn)B控件在A控件右面,則在B控件中設(shè)置layout_constraintLeft_toRightOf。意思就是說B控件的左面在A控件的右面

2. Margins
android:layout_marginStartandroid:layout_marginEndandroid:layout_marginLeftandroid:layout_marginTopandroid:layout_marginRightandroid:layout_marginBottom
這個與之前其他viewgroup屬性一致,不一樣的就是多了以下幾點:
layout_goneMarginStartlayout_goneMarginEndlayout_goneMarginLeftlayout_goneMarginToplayout_goneMarginRightlayout_goneMarginBottom
goneMargin屬性是指目標控件GONE掉之后,自身控件可以設(shè)置個margin值,這里有一點需要敲黑板,目標控件就是相對于的那個控件
3. Bias
- `layout_constraintHorizontal_bias``
layout_constraintVertical_bias
這個屬性的意思是可以使用偏差屬性調(diào)整定位以使一側(cè)偏向另一側(cè),即控件距離左右百分比(layout_constraintHorizontal_bias)和距離上下百分比(layout_constraintVertical_bias)

4. WRAP_CONTENT : enforcing constraints (*Added in 1.1*)
強制約束
app:layout_constrainedWidth=”true|false”app:layout_constrainedHeight=”true|false”
true代表防止約束失效,默認為false,比如:B在A的右邊app:layout_constraintLeft_toRightOf="@+id/a",但是當A的內(nèi)容越來越多并且超過了A到父控件最右的距離,此時就會約束失效使B的一部分出現(xiàn)了A的非右邊。如果B設(shè)置了該屬性為true,則B始終出現(xiàn)在A的右邊,不會發(fā)生約束失效
5. Ratio
app:layout_constraintDimensionRatio="H,16:9"
不用多說百分比布局是android中常用的一種適配布局H或W則代表以高或?qū)挒榛鶞?/p>
6. Guideline
layout_constraintGuide_begin 距離父容器起始位置的距離(左側(cè)或頂部)
layout_constraintGuide_end 距離父容器結(jié)束位置的距離(右側(cè)或底部)
layout_constraintGuide_percent 距離父容器寬度或高度的百分比
其實很好理解,比如
<android.support.constraint.Guideline
android:id="@+id/guide1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/guide1" />
則表示在垂直方向上畫一根基準線(只是參考線,并不進行view繪制)然后其他控件可以根據(jù)這條線進行放置

7. Barrier
<android.support.v7.widget.AppCompatButton
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="a" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bbbbbbbbbbbbbbb"
app:layout_constraintTop_toBottomOf="@+id/a" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c"
app:layout_constraintLeft_toRightOf="@+id/barrier" />
<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="a,b" />

顯而易見,你可以把他看做一個容器constraint_referenced_ids=控件id,然后把這些控件看做一個整體
8. Group
它和Barrier有異曲同工之處,相同的都是你可以把他們看做一個容器,不同的是他是控制整個容器之中的所有的控件的可見或者不可見,比如android:visibility="gone",那它所包裹的左右控件都會gone。
當然ConstraintLayout的Api不止這些,需要我們真真切切的去使用它,你會發(fā)現(xiàn)它真的很好用~
參考
Android開發(fā)文檔 [Developer][https://developer.android.com/reference/android/support/constraint/ConstraintLayout]