Android ConstraintLayout 使用詳解

ConstraintLayout(約束佈局)已經推出有一段時間了,在 Android Studio 中也作爲了默認佈局,能夠減少佈局的層級並改善佈局性能,因此很有必要來研究下其功能與使用方法

ConstraintLayout 能夠靈活地定位和調整子View的大小,子 View 依靠約束關係來確定位置。在一個約束關係中,需要有一個 Source(源)以及一個 Target(目標),Source 的位置依賴於 Target,可以理解爲“通過約束關係,Source 與 Target鏈接在了一起,Source 相對於 Target 的位置便是固定的了

一、基本屬性

ConstraintLayout 最基本的屬性控制有以下幾個,即 layout_constraintXXX_toYYYOf 格式的屬性,即將“View A”的方向 XXX 置於 “View B”的方向 YYY 。當中,View B 可以是父容器即 ConstraintLayout ,用“parent”來表示

  • layout_constraintBaseline_toBaselineOf (View A 內部文字與 View B 內部文字對齊)
  • layout_constraintLeft_toLeftOf (View A 與 View B 左對齊)
  • layout_constraintLeft_toRightOf (View A 的左邊置於 View B 的右邊)
  • layout_constraintRight_toLeftOf (View A 的右邊置於 View B 的左邊)
  • layout_constraintRight_toRightOf
  • layout_constraintTop_toTopOf
  • layout_constraintTop_toBottomOf
  • layout_constraintBottom_toTopOf
  • layout_constraintBottom_toBottomOf
  • layout_constraintStart_toEndOf
  • layout_constraintStart_toStartOf
  • layout_constraintEnd_toStartOf
  • layout_constraintEnd_toEndOf

各個方位的參照圖可以看以下圖片

ConstraintLayout 的這些屬性是爲控件添加了某個方向的約束力,根據某個方向約束力的“有無”或“強弱”,控件會位於不同的位置
例如,看以下代碼,根據某個方向的約束的不同,控件會居中或者不會

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.czy.constraintlayoutdemo.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:layout_margin="20dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tv2"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:background="#68b0f9"
        android:gravity="center"
        android:text="沒有設置底部約束,所以不會居於中間"
        app:layout_constraintLeft_toRightOf="@+id/tv1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/tv1"/>

    <TextView
        android:id="@+id/tv3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:background="#984ff7"
        android:gravity="center"
        android:text="上下均設置了約束,所以會居於中間"
        app:layout_constraintBottom_toBottomOf="@+id/tv1"
        app:layout_constraintLeft_toRightOf="@+id/tv1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv2"/>

</android.support.constraint.ConstraintLayout>

可以看到,藍色方塊由於只設置了頂部約束而沒有設置底部約束,所有藍色方塊只會與黃色方塊頂部對齊,而紫色方塊由於上下均設置了約束,且約束力的“強度”相同,所以紫色方塊會呈現居中效果

二、約束力的強度

那麼,約束力的“強度”該如何設定呢?可以依靠 layout_constraintHorizontal_bias 和 layout_constraintVertical_bias 兩個屬性,即用來設置控件在水平和垂直方向的偏移量
例如,以下佈局中, TextView 應該是處於水平和垂直兩個方向居中的

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.czy.constraintlayoutdemo.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

而在以下代碼中,黃色方塊左側所佔的剩餘空間從50%變成了100%,上側所佔的剩餘空間從50%變成了10%

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.czy.constraintlayoutdemo.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.1" />

</android.support.constraint.ConstraintLayout>

三、Visibility 屬性

在以前的佈局中,如果 View 的 visibility 屬性設置爲 gone,那麼其他原本依賴該 View 來參照定位的屬性都會失效,而在 ConstraintLayout 佈局中會有所不同
例如,在以下佈局中,紅色方塊位於屏幕右上角與黃色方塊左下角形成的矩形的中間位置

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.czy.constraintlayoutdemo.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#fa6e61"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="@+id/tv1"
        app:layout_constraintLeft_toLeftOf="@+id/tv1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

如果將黃色方塊的 visibility 屬性設置爲 gone,那麼,紅色方塊的位置會發生變化。可以理解爲黃色方塊縮小爲一個不可見的小點,位於其原先位置的中間,而紅色方塊則改爲依照該點來進行定位

此時,紅色方塊可以依靠 layout_goneMarginBottom 、layout_goneMarginStart等屬性來設置與黃色方塊之間的邊距,這類屬性只有在黃色方塊的 visibility 屬性設置爲gone時纔會生效

四、寬高比

在其他佈局中,如果想根據屏幕的寬度來爲 View 設置固定的寬高比的話是比較麻煩的,但在 ConstraintLayout 中可以直接設置
例如,如果想實現一個固定寬高比的頂部標題欄的話,可以將寬和高設置爲 0dp,然後爲其設置 app:layout_constraintDimensionRatio 屬性,設定寬高比爲16:7

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.czy.constraintlayoutdemo.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintDimensionRatio="h,16:7"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="#5476fd"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="W,15:25"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv1" />

</android.support.constraint.ConstraintLayout>

要使用 layout_constraintDimensionRatio 屬性,需要至少設置寬度或者高度爲 0dp,寬高的尺寸比例可以通過“float值”或者“寬度:高度”的形式來設置
如果寬度和高度都是 0dp ,系統會使用滿足所有約束條件和寬高比率值的最大尺寸
如果要根據其中一個尺寸來約束另外一個尺寸,則可以在比率值的前面添加 W 或者 H 來指明約束寬度或者高度

五、設置控件之間的寬高佔比

ConstraintLayout 也可以像 LinearLayout 一樣爲子控件設置 layout_weight 屬性,從而控件子控件之間的寬高佔比

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.czy.constraintlayoutdemo.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="1.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tv2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="#55e4f4"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/tv1"
        app:layout_constraintRight_toLeftOf="@+id/tv3"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="#f186ad"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/tv2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

六、錨向指示線

當需要一個任意位置的錨點時,可以使用指示線(Guideline)來幫助定位,指示線實際上是 View 的子類,使用方式和普通的 View 相同,但指示線有着如下的特殊屬性:

  • 寬度和高度均爲0
  • 可見性爲 View.GONE

即指示線只是爲了幫助其他 View 定位而存在,實際上並不會出現在實際界面中
例如,如下代碼加入了兩條指示線,可以選擇使用百分比或實際距離來設置指示線的位置,此外也可以通過 orientation 屬性來設置指示線的方向

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.czy.constraintlayoutdemo.MainActivity">

    <android.support.constraint.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="100dp" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintLeft_toRightOf="@+id/guideline1"
        app:layout_constraintTop_toBottomOf="@+id/guideline2" />

</android.support.constraint.ConstraintLayout>

設置橫向指示線距離頂部 100dp

豎向指示線設置其距離百分比爲 0.5,所以是位於屏幕的中間位置

七、Chains鏈

Chain 鏈比較難描述,它是一種特殊的約束,可以爲多個通過 chain 鏈連接的 View 來分發剩餘空間位置,類似於 LinearLayout 中的權重比 weight 屬性,但 Chains 鏈要強大得多

如果幾個View之間通過雙向連接而互相約束對方的位置,那麼將其視爲鏈條

例如,以下佈局代碼所呈現出來的效果,就可以稱爲一條鏈條,在這條鏈的最左側的元素成爲鏈頭,可以在其身上設置一些屬性以決定這個鏈的展示效果

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.czy.constraintlayoutdemo.MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="#f5ec7e"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tv2"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="0dp"
        android:background="#ff538c"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintLeft_toRightOf="@+id/tv1"
        app:layout_constraintRight_toLeftOf="@+id/tv3"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:background="#41c0ff"
        android:gravity="center"
        android:text="Hello World!"
        app:layout_constraintLeft_toRightOf="@+id/tv2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

鏈條分爲水平鏈條和豎直鏈條兩種,分別用 layout_constraintHorizontal_chainStyle 和 layout_constraintVertical_chainStyle 兩個屬性來設置
屬性值有以下三種:

  • spread
  • spread_inside
  • packed

默認值爲 spread

可以通過下圖來理解各個參數值的含義

當參數值爲 spread 以及控件寬度非 0 時

 android:layout_width="wrap_content"
 app:layout_constraintHorizontal_chainStyle="spread"

當參數值爲 spread 以及控件寬度爲 0 時

 android:layout_width="0dp"
 app:layout_constraintHorizontal_chainStyle="spread"

當參數值爲 packed 以及控件寬度非0時

 android:layout_width="wrap_content"
 app:layout_constraintHorizontal_chainStyle="packed"

當參數值爲 spread_inside 以及控件寬度非0時

 android:layout_width="wrap_content"
 app:layout_constraintHorizontal_chainStyle="spread_inside"



原文鏈接:https://www.jianshu.com/p/b884b8c46584

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章