ConstraintLayout基礎介紹

Code4Android發表於2017-09-19

自去年Google I/O 大會發布ConstraintLayout至今,已有一年多的時間,但是並沒有普及開來,瞭解過ConstraintLayout佈局的人知道,它的效能的確提升了不少。在前不久,Google 開發者部落格釋出了一篇文章Understanding the performance benefits of ConstraintLayout中文地址)詳細分析ConstraintLayout效能的優勢,感興趣的朋友可以去看看。

當然自己之前也沒有認識到ConstraintLayout佈局的效能優勢,所以從這篇文章開始由淺入深詳細介紹ConstraintLayout的屬性及使用,也讓自己對ConstraintLayout也有一個更全面的認識,今天的這篇文章主要介紹佈局的一些屬性,學習地址是Google文件

配置

在使用ConstraintLayout之前我們需要在我們app下的gradle檔案新增ConstraintLayout依賴,截止到目前ConstraintLayout的最新版本是1.0.2.

dependencies {
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
}複製程式碼

#Relative positioning
相對定位的效果和RelativeLayout佈局有異曲同工之處,只不過要比RelativeLayout強大。約束能允許我們指定一個控制元件相對於另一個控制元件的位置,通過一些屬性我們可以對元件進行水平或者垂直排列。例如當我們使用含有Left, Right, Start 或者End關鍵詞詞屬性進行定位時即是對元件進行水平方向排列,同理 top, bottom 和 text baseline(文字的基線位置)就是垂直方向排列。具體相對定位的一些屬性組合如下

  • 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

對於上面這些屬性的值有兩種,一種就是同層級元件ID,還有就是parent,當值為parent時即是相對於父佈局進行定位。

<?xml version="1.0" encoding="utf-8"?>
<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/textView1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="textView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="textView2"
        app:layout_constraintLeft_toRightOf="@+id/textView1" />
</android.support.constraint.ConstraintLayout>複製程式碼

例如上面的佈局,我們使用app:layout_constraintLeft_toRightOf="@+id/textView1"將textView2的左邊和textView1的右邊對齊,效果圖

image.png
image.png

那麼當textView2屬性設定為

    app:layout_constraintTop_toBottomOf="@+id/textView1"
    app:layout_constraintLeft_toRightOf="@+id/textView1"複製程式碼

效果圖:

image.png
image.png

當textView1設定屬性 app:layout_constraintRight_toLeftOf="parent"
textView2設定 app:layout_constraintLeft_toLeftOf="parent"時,

效果圖:

image.png
image.png

Margins

對於margin值我們都不陌生,因為我們經常使用,有以下幾種

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

需要注意的是此margin只對於設定了約束的地方起作用,如下佈局

<?xml version="1.0" encoding="utf-8"?>
<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/textView1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:text="textView1" />
</android.support.constraint.ConstraintLayout>複製程式碼

我們只設定了TextView的左邊和父佈局左邊約束,當我們設定了左邊和上邊的margin值都為10時,發現只有左邊的邊距生效,而上邊的變化沒有發生作用,這也就驗證了邊距只對有約束行為的地方起作用。

除了我們常用的margin設定屬性外,ConstraintLayout 還提供了一些特有的margin設定。先看下面佈局

<?xml version="1.0" encoding="utf-8"?>
<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/textView1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="textView1"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="textView2"
        app:layout_constraintLeft_toRightOf="@+id/textView1" />
</android.support.constraint.ConstraintLayout>複製程式碼

上面textview2在textview1的右邊,那麼當我們由於某種需求將textview1設定了 View.GONE隱藏該控制元件,那麼此時textview2將跑到位置將顯示在左上角,如果我們在隱藏textview時而保持textview2位置不變。在之前的應用中能稍微比較麻煩一點,但是ConstraintLayout 給我們提供了layout_goneMargin**類的屬性,該屬性是表示約束隱藏時的margin值。例如上面的textView2我們增加app:layout_goneMarginLeft="100dp"屬性就可以保持當約束textView1隱藏時而保持textview2位置不變。

需要注意的一點是如果某個約束設定了View.GONE,相當於這個元件寬和高為0,約束佈局中相當於一個點,其他設定的約束依然有效。

Centering positioning and bias

    <TextView
        android:id="@+id/textView1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="textView1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />複製程式碼

如果約束佈局中我們新增textview並設左邊和父佈局左邊,右邊和父佈局右邊對齊,並設定寬度100dp,那麼此時效果是怎樣的呢。我們發現textview居中顯示了,這就是約束佈局的居中定位。
其實我們可以理解為父佈局對textview左邊和右邊都有一個拉力,由於預設這個力大小相同就到顯示到中間位置。

image.png
image.png

在上面由於兩個相反的約束,而使元件居中,在協同佈局中還提供了bias,通過屬性layout_constraintHorizontal_bias或者layout_constraintVertical_bias設定元件偏向水平或者垂直的某一測,,預設情況下該值是0.5(50%).例如我們設定textview屬性

        app:layout_constraintHorizontal_bias="0.3"複製程式碼

此時元件偏向左邊

image.png
image.png

Dimensions constraints

當我們的協調佈局的子元件設定wrap_content時,我們可以通過android:minWidth或者android:minHeight 屬性設定最小寬度或者高度的約束。minWidth/minHeight只有寬度或者高度為wrap_content才有作用。

對於android:layout_height /android:layout_width屬性,它的值只有三種情況

  • 使用指定的大小或者指定大小的引用,如100dp
  • WRAP_CONTENT,此時根據內容自己計算大小
  • 0dp,該值相當於MATCH_CONSTRAINT。

需要注意的是在約束佈局中MATCH_PARENT 屬性值不在支援,例如在上面的TextView我們設定layout_width分別是100dp,0dp,0dp(marginLeft :20dp)大致效果如圖a,b,c

image.png
image.png

Ratio

比例約束可以約束我們控制元件的寬高比,例如下面示例

    <TextView
        android:id="@+id/textView1"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="textView1"
        app:layout_constraintDimensionRatio="2:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />複製程式碼

設定寬度為100dp,高度為0dp,此時設定了寬高比是2:1,則寬度會自動約束調整為50dp。
如果我們將上面的寬和高度調換,寬為0dp,高100dp,此時最終寬度為200,高度100(寬:高=2:1)

在上面介紹的的寬高比約束是單維度的,那麼如果我們的寬和高都有約束,都設定為0dp,在這種情況下,系統會使用滿足所有約束條件和比率的最大尺寸。當然我們也可以在比例值前面加 W 或者 H 來分別約束寬度或者高度,如H,2:1。

Chains

鏈是一種特殊的約束它能讓多個該鏈連線的 多個Views 平分剩餘空間位置
如下我們建立一個水平鏈

<?xml version="1.0" encoding="utf-8"?>
<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"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:background="@color/btnnormal"
        android:gravity="center"
        android:text="A"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/B" />

    <TextView
        android:id="@+id/B"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="B"
        app:layout_constraintLeft_toRightOf="@+id/A"
        app:layout_constraintRight_toLeftOf="@+id/C" />

    <TextView
        android:id="@+id/C"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:background="@color/btnnormal"
        android:gravity="center"
        android:text="C"
        app:layout_constraintLeft_toRightOf="@+id/B"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>複製程式碼

上面程式碼的效果圖如下,A和C相對於父元件的左邊和右邊有一個約束,A和B,B和C之間兩兩相互約束,我們還需要知道的是對我們稱鏈的第一個元素元件為鏈頭。如下圖A就是該鏈的鏈頭

image.png
image.png

鏈的模式

對於鏈頭我們可以通過屬性layout_constraintHorizontal_chainStyle(layout_constraintVertical_chainStyle)設定,該屬性有三個值,spread ,spread_inside ,packed

  • CHAIN_SPREAD
    預設情況下該屬性預設值是spread 。它的間隙將平分剩餘空間,如上圖所示
  • CHAIN_SPREAD_INSIDE
    spread_inside值會把兩邊最邊緣的兩個 View 靠邊顯示,然後讓剩餘的 Views 在剩餘的空間內平分間隙。當面設定spread_inside時效果圖如下

image.png
image.png

  • CHAIN_PACKED
    還有一種值是packed ,它表示將view之間緊挨著顯示,並且全體居中顯示,設定該模式後效果圖如下

image.png
image.png

除此之外,我們可以通過設定layout_constraintHorizontal_bias屬性來調整整體的位置。預設情況居中,也就是該值為0.5,例如我們將該值設定0.3,則實現效果如下

image.png
image.png

  • Weighted chain
    在官方文件中還介紹了一種模式是權重模式,在CHAIN_SPREAD 模式中,如果我們設定控制元件的寬或者高設定MATCH_CONSTRAINT即0dp,它們將按權重平分佔滿父控制元件的寬或者高,對於權重的設定是屬性和我們線性佈局設定權重達到一樣的效果,例如在上面的三個TextView,我們設定寬度都為0dp,並設定鏈樣式為spread(或spread_inside),此時效果 圖如下

image.png
image.png

如果我們給A和C設定下面屬性

        app:layout_constraintHorizontal_weight="1"複製程式碼

給B設定屬性

        app:layout_constraintHorizontal_weight="2"複製程式碼

那麼此時A,B,C的寬度為1:2:1比例佔滿父元件寬度。設定權重時鏈樣式不能設定為packed (設定後寬度會收縮為0)

參照線Guideline

Guideline用於輔助我們對View進行定位,以及設定約束,它不會再真正的顯示,只是起到輔助作用,常用屬性如下

  • android:orientation
    該屬性可以指定輔助線是垂直還是水平線,它有兩個值即vertical,horizontal,
  • layout_constraintGuide_begin/layout_constraintGuide_end
    用來設定對齊父元件的 start 或者end邊緣的距離,
  • layout_constraintGuide_percent
    該值是0.0到1.0之間,用來設定輔助線在父佈局的位置,例如設定0.5,就相當於在父檢視寬度的中間50%。

      <android.support.constraint.Guideline
          android:id="@+id/guideline_h"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          app:layout_constraintGuide_percent="0.5" />
    
      <android.support.constraint.Guideline
          android:id="@+id/guideline_v"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          app:layout_constraintGuide_percent="0.5" />
    
      <TextView
          android:id="@+id/tvguide"
          android:layout_width="50dp"
          android:layout_height="50dp"
          android:background="@color/red"
          android:gravity="center"
          android:text="Guide"
          app:layout_constraintBottom_toTopOf="@+id/guideline_h"
          app:layout_constraintLeft_toLeftOf="@+id/guideline_v" />複製程式碼

    在上面我們在約束佈局中建立兩個輔助線,分別是垂直和水平的輔助線,並將textView的下面和水平輔助線的上面對齊,將TextView的左邊和垂直輔助線左邊對齊。這樣我們可以使用輔助線任意控制View的約束位置。

image.png
image.png

好了今天ConstraintLayout的基礎知識就介紹到這裡了,下一篇文章將介紹ConstraintLayout佈局中Behavior的相關知識,如文中有錯誤歡迎指出。Hava a wonderful day。

相關文章