天天看點

Android ConstraintLayout 使用鍊控制線性組

使用鍊控制線性組

鍊是一組視圖,這些視圖通過雙向位置限制條件互相連結到一起。鍊中的視圖可以垂直或水準分布。

    1. Spread:視圖是均勻分布的(在考慮外邊距之後)。這是預設值。
    1. Spread inside:第一個和最後一個視圖固定在鍊兩端的限制邊界上,其餘視圖均勻分布。
    1. Weighted:當鍊設定為 spread 或 spread inside 時,您可以通過将一個或多個視圖設定為“match constraints”(0dp) 來填充剩餘空間。預設情況下,設定為“match constraints”的每個視圖之間的空間均勻分布,但您可以使用 layout_constraintHorizontal_weight 和 layout_constraintVertical_weight 屬性為每個視圖配置設定重要性權重。如果您熟悉線性布局中的 layout_weight 的話,就會知道該樣式與它的原理是相同的。是以,權重值最高的視圖獲得的空間最大;相同權重的視圖獲得同樣大小的空間。
    1. Packed:視圖打包在一起(在考慮外邊距之後)。 然後,您可以通過更改鍊的頭視圖偏差調整整條鍊的偏差(左/右或上/下)。

鍊的“頭”視圖:水準鍊中最左側的視圖以及垂直鍊中最頂部的視圖。

鍊的“尾”視圖:水準鍊中最右(end)側的視圖以及垂直鍊中最底部的視圖。

要組成鍊,頭和尾必須進行設定。

以水準鍊為例

  • app:layout_constraintStart_toStartOf="parent"

  • app:layout_constraintEnd_toEndOf="parent"

水準鍊

預設均勻分布 Spread

4個TextView水準排開,均勻分布。

layout中簡單說來是互相作為參照物。

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="20dp"
        android:background="#fff">

        <TextView
            android:id="@+id/tv1"
            style="@style/ConSampleText"
            android:layout_marginEnd="8dp"
            android:background="#009688"
            android:gravity="center"
            android:text="R"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toStartOf="@+id/tv2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv2"
            style="@style/ConSampleText"
            android:background="#009688"
            android:gravity="center"
            android:text="u"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toStartOf="@+id/tv3"
            app:layout_constraintStart_toEndOf="@+id/tv1"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv3"
            style="@style/ConSampleText"
            android:background="#009688"
            android:gravity="center"
            android:text="s"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toStartOf="@+id/tv4"
            app:layout_constraintStart_toEndOf="@+id/tv2"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv4"
            style="@style/ConSampleText"
            android:background="#009688"
            android:gravity="center"
            android:text="t"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv3"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
           

style.xml

<style name="ConSampleText">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:padding">4dp</item>
        <item name="android:textColor">#fff</item>
        <item name="android:background">#3F51B5</item>
    </style>
           

Spread inside

如果需要Spread inside樣式,需要設定一下“頭”子view:

app:layout_constraintHorizontal_chainStyle="spread_inside"

<!-- ... -->
    <TextView
        android:id="@+id/tv1"
        style="@style/ConSampleText"
        android:background="#009688"
        android:gravity="center"
        android:text="R"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toStartOf="@+id/tv22"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!-- ... -->
           

Weighted

可以設定子view按比例配置設定剩餘空間。需要先把width設為0dp。

并且設定

app:layout_constraintHorizontal_weight

屬性。

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="20dp"
        android:background="#fff">

        <TextView
            android:id="@+id/tv1"
            style="@style/ConSampleText"
            android:layout_width="0dp"
            app:layout_constraintHorizontal_weight="2"
            android:background="#4CAF50"
            android:gravity="center"
            android:text="R"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toStartOf="@+id/tv2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv2"
            style="@style/ConSampleText"
            android:layout_width="0dp"
            android:background="#009688"
            app:layout_constraintHorizontal_weight="1"
            android:gravity="center"
            android:text="u"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toStartOf="@+id/tv3"
            app:layout_constraintStart_toEndOf="@+id/tv1"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv3"
            style="@style/ConSampleText"
            android:background="#4CAF50"
            android:gravity="center"
            android:text="s"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toStartOf="@+id/tv4"
            app:layout_constraintStart_toEndOf="@+id/tv2"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv4"
            style="@style/ConSampleText"
            android:background="#009688"
            android:gravity="center"
            android:text="t"
            android:textColor="#ffffff"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv3"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

           

packed

第一個子view設定

app:layout_constraintHorizontal_chainStyle="packed"

<!-- ... -->
    <TextView
        android:id="@+id/tv1"
        style="@style/ConSampleText"
        android:background="#009688"
        android:gravity="center"
        android:text="R"
        android:textColor="#ffffff"
        app:layout_constraintEnd_toStartOf="@+id/tv22"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!-- ... -->
           

5等分螢幕

等分螢幕的方法有很多種方式。例如加上guideline,設定好比例。這裡我們用鍊式來水準等分螢幕或父layout。

不論是平分,3等分或是5等分,方式都是類似的。以5等分為例。

切分的view,寬度設定為0dp,layout_constraintHorizontal_weight設定為一樣的,比如1。

頭視圖注意設定

app:layout_constraintHorizontal_chainStyle="spread"

app:layout_constraintStart_toStartOf="parent"

尾視圖注意設定

app:layout_constraintEnd_toEndOf="parent"

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="290dp"
        android:layout_marginTop="8dp"
        android:background="#ffffff">

        <RelativeLayout
            android:id="@+id/dc_start"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toStartOf="@id/dc_2"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintStart_toStartOf="parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="center"
                android:src="@drawable/pic_hj1" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/dc_2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toStartOf="@id/dc_3"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintStart_toEndOf="@id/dc_start">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="center"
                android:src="@drawable/pic_hj2" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/dc_3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toStartOf="@id/dc_4"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintStart_toEndOf="@id/dc_2">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="center"
                android:src="@drawable/pic_hj3" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/dc_4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toStartOf="@id/dc_tail"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintStart_toEndOf="@id/dc_3">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="center"
                android:src="@drawable/pic_hj4" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/dc_tail"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintStart_toEndOf="@+id/dc_4">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="center"
                android:src="@drawable/pic_hj5" />

        </RelativeLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

           

注意事項

以下是使用鍊時需要考慮的其他事項:

  • 視圖可以是水準鍊和垂直鍊的一部分,是以可以輕松建構靈活的網格布局。
  • 隻有當鍊的每一端都被限制到同一軸上的另一個對象時,鍊才能正常工作。
  • 雖然鍊的方向為垂直或水準,但使用其中一個方向不會沿該方向與視圖對齊。是以,請務必包含其他限制條件,以便使鍊中的每個視圖都能正确定位,例如對齊限制。

一個軟體工程師的記錄