全網(wǎng)最清晰的ConstraintLayout教程

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_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

看到這些猜也能猜出個大概比如layout_constraintLeft_toLeftOf就是說當前控件的Left在目標控件的Left上。如果目標控件為父控件則id可以直接寫成parent。比如要實現(xiàn)B控件在A控件右面,則在B控件中設(shè)置layout_constraintLeft_toRightOf。意思就是說B控件的左面在A控件的右面

圖片標題

2. Margins

  • android:layout_marginStart
  • android:layout_marginEnd
  • android:layout_marginLeft
  • android:layout_marginTop
  • android:layout_marginRight
  • android:layout_marginBottom

這個與之前其他viewgroup屬性一致,不一樣的就是多了以下幾點:

  • layout_goneMarginStart
  • layout_goneMarginEnd
  • layout_goneMarginLeft
  • layout_goneMarginTop
  • layout_goneMarginRight
  • layout_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]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 想看我更多文章:【張旭童的博客】http://www.itdecent.cn/p/9b6e12d8eea0 概述 ...
    張旭童閱讀 19,929評論 9 42
  • ConstraintLayout 是在 2016 年 Google 大會上推出的一個新的布局控件,眾所周知,Con...
    lijiankun24閱讀 5,215評論 3 33
  • 網(wǎng)傳constraintLayout(約束布局)的布局,性能和功能性相當優(yōu)越,雖然RelativeLayout用的...
    才華橫溢的王小白閱讀 3,197評論 0 16
  • ConstraintLayout 布局繼承自 ViewGroup,它可以很靈活的指定控件的位置和大小。 注意:Co...
    walker不抽煙閱讀 991評論 0 0
  • 今天看到之前離職的同事發(fā)了一篇簡書朋友圈,果斷贊賞表示支持。糖還沒來得及給她,就走了。。。文章拉到底部時發(fā)現(xiàn)底部會...
    測試2017閱讀 175評論 0 0

友情鏈接更多精彩內(nèi)容