1. 程式人生 > >Android ConstraintLayout約束佈局

Android ConstraintLayout約束佈局

1.簡介:

constraintLayout和RelativeLayout類似,但比RelativeLayout要強大多,它可以有效地解決佈局巢狀過多問題,我們平時編寫的介面,複雜的佈局總會伴隨著多層的巢狀,而巢狀越多,程式的效能也就越差;ConstraintLayout則是使用約束的方式來指定各個控制元件的位置和關係;

2.常用相對屬性:

layout_constraintTop_toTopOf       // 將所需檢視的頂部與另一個檢視的頂部對齊。 
layout_constraintTop_toBottomOf    // 將所需檢視的頂部與另一個檢視的底部對齊。 
layout_constraintBottom_toTopOf    // 將所需檢視的底部與另一個檢視的頂部對齊。 
layout_constraintBottom_toBottomOf // 將所需檢視的底部與另一個檢視的底部對齊。 layout_constraintLeft_toTopOf // 將所需檢視的左側與另一個檢視的頂部對齊。 layout_constraintLeft_toBottomOf // 將所需檢視的左側與另一個檢視的底部對齊。 layout_constraintLeft_toLeftOf // 將所需檢視的左邊與另一個檢視的左邊對齊。 layout_constraintLeft_toRightOf // 將所需檢視的左邊與另一個檢視的右邊對齊。 layout_constraintRight_toTopOf // 將所需檢視的右對齊到另一個檢視的頂部。
layout_constraintRight_toBottomOf // 將所需檢視的右對齊到另一個的底部。 layout_constraintRight_toLeftOf // 將所需檢視的右邊與另一個檢視的左邊對齊。 layout_constraintRight_toRightOf // 將所需檢視的右邊與另一個檢視的右邊對齊。 layout_constraintBaseline_toBaselineOf :文字底部對齊,與alignBaseLine屬性相似 layout_constraintStart_toEndOf :同left_toRightOf layout_constraintStart_toStartOf :同left_toLeftOf layout_constraintEnd_toStartOf :同right_toLeftOf layout_constraintEnd_toEndOf :同right_toRightOf

3.margins屬性

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

4.當前View與另一個View繫結後,另一個View的屬性設定為了Gone,則以下屬性會生效

layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

5.居中並設定權重,使view居中並且設定權重,同RelativeLayout的center_horizontal/vertical=“true”

設定方法:以橫向居中為例(豎向同理):
將ConstraintLayout的子View的屬性如下進行設定

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

6.設定權重layout_constraintHorizontal_bias(layout_constraintVertical_bias)

例如:下面將會使左側用30%的偏差,而不是預設的50%,使得左側將會縮短,與微件傾斜更靠近左側

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

7.尺寸限制

在ConstraintLayout最小尺寸,被使用ConstraintLayout時,其尺寸設定為WRAP_CONTENT

  • android:minWidth 設定的最小寬度為佈局
  • android:minHeight 設定最低高度佈局

注意:

ConstraintLayout 不支援match_parent屬性,但支援wrap_content屬性。如果你需要用match_parent,將寬度/高度指定為0dp,然後設定left_toleft,right_toRight為parent即可實現橫向充滿,同理設定豎向的

8.Ratio比例大小屬性

You can also define one dimension of a widget as a ratio of the other one. In order to do that, you need to have at least one constrained dimension be set to 0dp (i.e., MATCH_CONSTRAINT), and set the attribute layout_constraintDimentionRatio to a given ratio. For example:

當你的父控制元件為ConstraintLayout,可以利用這個屬性來控制當前View的寬高比。在利用這個屬性時,你必須指明一個方向上的大小為0dp,另一個方向可指定為明確的dp值也可以使用wrap_content這樣才能夠按照比例來為你擺放
例如:“寬度:高度

<Button 
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1" />

也可以這樣設定:

<ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintDimensionRatio="H,6:1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

把寬和高設定為0dp,但是通過Top_toTopOf、Bottom_toBottomOf指定高度,通過新增字母W(為約束的寬度)或H (用逗號分隔的前方約束的高度);既是高度已經指定大小,寬度按照對應比例分配大小;

9.Chains鏈

當設定屬性layout_constraintHorizontal_chainStyle或layout_constraintVertical_chainStyle一個鏈的第一個元素上,所述鏈的行為將根據指定的樣式(預設為改變CHAIN_SPREAD)。

CHAIN_SPREAD - 該元素將被傳播出去(預設方式)
加權鏈-在CHAIN_SPREAD模式下,如果一些小部件設定為MATCH_CONSTRAINT,他們將分裂的可用空間
CHAIN_SPREAD_INSIDE - 類似,但鏈條的終點不會散開
CHAIN_PACKED - 鏈條的元件將被一起包裝。那麼孩子的水平或垂直偏置屬性會影響包裝元件的定位