Android ConstraintLayout 構(gòu)建自適應(yīng)界面

原文鏈接

使用 ConstraintLayout 構(gòu)建自適應(yīng)界面

ConstraintLayout 可讓您使用扁平視圖層次結(jié)構(gòu)(無(wú)嵌套視圖組)創(chuàng)建復(fù)雜的大型布局。它與 RelativeLayout 相似,其中所有的視圖均根據(jù)同級(jí)視圖與父布局之間的關(guān)系進(jìn)行布局,但其靈活性要高于 RelativeLayout,并且更易于與 Android Studio 的布局編輯器配合使用。

本文展示約束條件中的幾種用法。

約束條件

創(chuàng)建約束條件時(shí),請(qǐng)注意以下規(guī)則:

  • 每個(gè)視圖都必須至少有兩個(gè)約束條件:一個(gè)水平約束條件,一個(gè)垂直約束條件。
  • 只能在共用同一平面的約束手柄與定位點(diǎn)之間創(chuàng)建約束條件。因此,視圖的垂直平面(左側(cè)和右側(cè))只能約束在另一個(gè)垂直平面上;而基準(zhǔn)線則只能約束到其他基準(zhǔn)線上。
  • 每個(gè)約束句柄只能用于一個(gè)約束條件,但您可以在同一定位點(diǎn)上創(chuàng)建多個(gè)約束條件(從不同的視圖)。

gradle 引入

引入constraintlayout庫(kù)

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

constraintlayout使用

在layout中使用android.support.constraint.ConstraintLayout,如下示例

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintTop_toTopOf="parent">

        <!-- child view layout -->

    </androidx.constraintlayout.widget.ConstraintLayout>

style中新建一個(gè)樣式,方便后面操作

    <!-- con layout 示例text -->
    <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>

若子view沒有添加約束,則會(huì)跑到父constraintlayout的(0,0)位置

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/c1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#f0f0f0"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            style="@style/ConSampleText"
            android:text="No rule, jump to (0,0)" />

    </androidx.constraintlayout.widget.ConstraintLayout>

對(duì)齊,屬性說明

定位時(shí)使用到諸如app:layout_constraintStart_toStartOf或者app:layout_constraintTop_toTopOf屬性。

app:layout_constraintStart_toStartOf,里面有2個(gè)Start字眼。
第一個(gè)Start表示自身的起始位置(默認(rèn)是左邊)。第二個(gè)toStartOf表示對(duì)齊參照物的起始位置。

app:layout_constraintTop_toTopOf也類似。與參照物頂部對(duì)齊。

指定位置的字眼,如TopBottomEndStart,它們組合使用可用來(lái)確定相對(duì)位置:app:layout_constraint{}_to{}Of

相對(duì)父layout的定位

將子view對(duì)齊到父layout的各個(gè)邊緣位置。

約束的參考對(duì)象是parent

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/c2"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_marginTop="20dp"
        android:background="#f0f0f0"
        app:layout_constraintTop_toBottomOf="@id/c1">

        <!-- 相對(duì)父layout的邊緣定位 -->

        <TextView
            style="@style/ConSampleText"
            android:text="居中"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="左上角"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="左下角"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="右下角"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="右上角"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="頂部水平居中"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="底部水平居中"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="左邊垂直居中"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="右邊垂直居中"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
手機(jī)截圖

基線對(duì)齊

將一個(gè)視圖的文本基線與另一視圖的文本基線對(duì)齊。

可以使用app:layout_constraintBaseline_toBaselineOf屬性設(shè)置基線對(duì)齊。

示例

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/c21"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#f0f0f0"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tv1"
            style="@style/ConSampleText"
            android:layout_marginStart="10dp"
            android:text="an"
            android:textSize="13sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv2"
            style="@style/ConSampleText"
            android:layout_marginStart="10dp"
            android:text="Rust"
            android:textSize="20sp"
            app:layout_constraintBaseline_toBaselineOf="@id/tv1"
            app:layout_constraintStart_toEndOf="@+id/tv1" />

        <TextView
            android:id="@+id/tv3"
            style="@style/ConSampleText"
            android:layout_marginStart="10dp"
            android:text="Fisher"
            android:textSize="24sp"
            app:layout_constraintBaseline_toBaselineOf="@id/tv1"
            app:layout_constraintStart_toEndOf="@+id/tv2" />

    </androidx.constraintlayout.widget.ConstraintLayout>

示例圖


baseline

引導(dǎo)線約束 Guideline

在ConstraintLayout中添加引導(dǎo)線,可以方便定位。其他View可以引導(dǎo)線作為參考位置。

添加Guideline,需要確定它的方向,分別是垂直和水平。

  • android:orientation="vertical"
  • android:orientation="horizontal"

比例定位

這里按比例來(lái)定位,使用app:layout_constraintGuide_percent
需要指定比例值,例如app:layout_constraintGuide_percent="0.5"

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/c3"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_marginTop="40dp"
        android:background="#f0f0f0"
        app:layout_constraintTop_toBottomOf="@id/c2">

        <!-- 引導(dǎo)線約束: 相對(duì)父layout 按比例定位 -->

        <!-- 垂直引導(dǎo)線 Guideline -->
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/g1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.33" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/g2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.5" />

        <TextView
            android:id="@+id/t1"
            style="@style/ConSampleText"
            android:text="垂直的1/3引導(dǎo)線后"
            app:layout_constraintStart_toStartOf="@id/g1"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/t2"
            style="@style/ConSampleText"
            android:text="垂直的1/3引導(dǎo)線前"
            app:layout_constraintEnd_toStartOf="@id/g1"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/t3"
            style="@style/ConSampleText"
            android:layout_marginTop="2dp"
            android:text="垂直的1/3引導(dǎo)線居中"
            app:layout_constraintEnd_toEndOf="@id/g1"
            app:layout_constraintStart_toStartOf="@id/g1"
            app:layout_constraintTop_toBottomOf="@id/t2" />

        <TextView
            android:id="@+id/th1"
            style="@style/ConSampleText"
            android:text="水平的1/2引導(dǎo)線居中"
            app:layout_constraintBottom_toBottomOf="@id/g2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/g2" />

        <TextView
            android:id="@+id/th2"
            style="@style/ConSampleText"
            android:layout_marginStart="2dp"
            android:text="水平的1/2引導(dǎo)線上面"
            app:layout_constraintBottom_toBottomOf="@id/g2"
            app:layout_constraintStart_toEndOf="@id/th1" />

        <TextView
            android:id="@+id/th3"
            style="@style/ConSampleText"
            android:layout_marginStart="2dp"
            android:text="水平的1/2引導(dǎo)線下面"
            app:layout_constraintStart_toEndOf="@id/th1"
            app:layout_constraintTop_toBottomOf="@id/g2" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/gv75"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.75" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/gh75"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.75" />

        <TextView
            style="@style/ConSampleText"
            android:layout_marginStart="2dp"
            android:text="(0.75,0.75)"
            app:layout_constraintBottom_toBottomOf="@id/gh75"
            app:layout_constraintEnd_toEndOf="@id/gv75"
            app:layout_constraintStart_toStartOf="@id/gv75"
            app:layout_constraintTop_toTopOf="@id/gh75" />

        <TextView
            style="@style/ConSampleText"
            android:layout_marginStart="2dp"
            android:text="(0.33, 0.75)"
            app:layout_constraintBottom_toBottomOf="@id/gh75"
            app:layout_constraintEnd_toEndOf="@id/g1"
            app:layout_constraintStart_toStartOf="@id/g1"
            app:layout_constraintTop_toTopOf="@id/gh75" />

    </androidx.constraintlayout.widget.ConstraintLayout>
手機(jī)截圖
as預(yù)覽圖

具體數(shù)值

我們也可以使用app:layout_constraintGuide_end或者app:layout_constraintGuide_begin來(lái)指定具體的數(shù)值。

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/c4"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="30dp"
        android:background="#f0f0f0"
        app:layout_constraintTop_toBottomOf="@id/c3">

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="10dp" />

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_end="10dp" />

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_begin="10dp" />

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_end="10dp" />

    </androidx.constraintlayout.widget.ConstraintLayout>

as預(yù)覽圖

as預(yù)覽圖

屏障約束

與引導(dǎo)線類似,屏障是一條隱藏的線,您可以用它來(lái)約束視圖。屏障不會(huì)定義自己的位置;相反,屏障的位置會(huì)隨著其中所含視圖的位置而移動(dòng)。
如果您希望將視圖限制到一組視圖而不是某個(gè)特定視圖,這就非常有用。


image

豎直屏障示例

這是一個(gè)豎直屏障的例子。barrier1以tv221和tv222作為參考。

設(shè)置app:barrierDirection="end",并且設(shè)置tv223在它的右側(cè)。

也就是barrier1會(huì)被tv221和tv222“推”著走。

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/c22"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginTop="10dp"
        android:background="#f3f3f3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/c21">

        <TextView
            android:id="@+id/tv221"
            style="@style/ConSampleText"
            android:layout_marginStart="10dp"
            android:text="an"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv222"
            style="@style/ConSampleText"
            android:layout_marginTop="4dp"
            android:text="RustFisher"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv221" />

        <TextView
            style="@style/ConSampleText"
            android:layout_marginTop="4dp"
            android:text="沒有以此作為參考,不管這個(gè)有多長(zhǎng)"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv222" />

        <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrier1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="end"
            app:constraint_referenced_ids="tv221 ,tv222" />

        <TextView
            android:id="@+id/tv223"
            style="@style/ConSampleText"
            android:layout_marginStart="10dp"
            android:text=".com"
            app:layout_constraintStart_toEndOf="@id/barrier1"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

預(yù)覽圖

調(diào)整約束偏差

對(duì)某個(gè)視圖的兩側(cè)添加約束條件(并且同一維度的視圖尺寸為“fixed”或者“wrap Content”)時(shí),則該視圖在兩個(gè)約束條件之間居中且默認(rèn)偏差為 50%。
可以通過設(shè)置屬性來(lái)調(diào)整偏差。

  • app:layout_constraintVertical_bias
  • app:layout_constraintHorizontal_bias

例如

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/c23"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_marginTop="10dp"
        android:background="#f3f3f3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/c22">

        <TextView
            style="@style/ConSampleText"
            android:text="Rust"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:text="0.33,0.33"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.33"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.33" />

        <TextView
            style="@style/ConSampleText"
            android:text="0.8,0.8"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.8"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.8" />

    </androidx.constraintlayout.widget.ConstraintLayout>

as預(yù)覽圖

調(diào)整視圖尺寸

這里調(diào)整的是子view的尺寸。

Match Constraints
視圖會(huì)盡可能擴(kuò)展,以滿足每側(cè)的約束條件(在考慮視圖的外邊距之后)。
不過,您可以使用以下屬性和值修改該行為(這些屬性僅在您將視圖寬度設(shè)置為“match constraints”時(shí)才會(huì)生效):

layout_constraintWidth_default

  • spread:盡可能擴(kuò)展視圖以滿足每側(cè)的約束條件。這是默認(rèn)行為。

  • wrap:僅在需要時(shí)擴(kuò)展視圖以適應(yīng)其內(nèi)容,但如有約束條件限制,視圖仍然可以小于其內(nèi)容。因此,它與使用 Wrap Content(上面)之間的區(qū)別在于,將寬度設(shè)為 Wrap Content 會(huì)強(qiáng)行使寬度始終與內(nèi)容寬度完全匹配;而使用 layout_constraintWidth_default 設(shè)置為 wrap 的Match Constraints 時(shí),視圖可以小于內(nèi)容寬度。

  • layout_constraintWidth_min 該視圖的最小寬度采用 dp 維度。

  • layout_constraintWidth_max 該視圖的最大寬度采用 dp 維度。

layout中設(shè)置 android:layout_width="0dp"android:layout_height="0dp"
確定好周圍的參照線。

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/c24"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:background="#009C8D"
        app:layout_constraintTop_toBottomOf="@id/c23">

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/v50"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/h50"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.5" />

        <TextView
            style="@style/ConSampleText"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="30dp"
            android:gravity="center"
            android:text="R"
            app:layout_constraintBottom_toTopOf="@id/h50"
            app:layout_constraintEnd_toStartOf="@id/v50"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="30dp"
            android:gravity="center"
            android:text="U"
            app:layout_constraintBottom_toTopOf="@id/h50"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/v50"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            style="@style/ConSampleText"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="30dp"
            android:gravity="center"
            android:text="S"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/v50"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/h50" />

        <TextView
            style="@style/ConSampleText"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="30dp"
            android:gravity="center"
            android:text="T"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/v50"
            app:layout_constraintTop_toBottomOf="@id/h50" />

    </androidx.constraintlayout.widget.ConstraintLayout>
as

運(yùn)行截圖

將尺寸設(shè)置為比例

比例為寬比高 width:height。如果寬高其中一個(gè)設(shè)置了大于0的具體值或wrap_content,可以其為標(biāo)準(zhǔn)來(lái)調(diào)整另一個(gè)尺寸參數(shù)。

示例1

設(shè)置layout_width="40dp"android:layout_height="0dp",比例為3:2。
以寬40dp為基準(zhǔn),按比例調(diào)整高度。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="@dimen/con_card_size"
    android:layout_height="@dimen/con_card_size"
    android:background="#f0f0f0">

    <TextView
        android:layout_width="40dp"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:background="#2196F3"
        android:gravity="center"
        android:text="3:2"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="3:2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
預(yù)覽圖
示例2

高度40dp,比例3:2,自動(dòng)調(diào)整寬度。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="@dimen/con_card_size"
    android:layout_height="@dimen/con_card_size"
    android:layout_marginStart="20dp"
    android:background="#f0f0f0">

    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:background="#009688"
        android:gravity="center"
        android:text="3:2"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="3:2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
as預(yù)覽圖
寬高都設(shè)為0

寬高都設(shè)置為0dp,比例為3:2。會(huì)嘗試填滿整個(gè)父layout。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="@dimen/con_card_size"
    android:layout_height="@dimen/con_card_size"
    android:layout_marginStart="20dp"
    android:background="#f0f0f0">

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#3F51B5"
        android:gravity="center"
        android:text="3:2"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="3:2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
預(yù)覽圖
寬wrap_content,高度0

layout_width -> wrap_content,高度為0,比例1:2。以寬度為基準(zhǔn)。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="@dimen/con_card_size"
    android:layout_height="@dimen/con_card_size"
    android:background="#f0f0f0">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:background="#009688"
        android:gravity="center"
        android:text="1:2"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
as
高度wrap_content,寬度0

比例5:2,會(huì)以高度為基準(zhǔn)。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="@dimen/con_card_size"
    android:layout_height="@dimen/con_card_size"
    android:layout_marginStart="20dp"
    android:background="#f0f0f0"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:background="#009688"
        android:gravity="center"
        android:text="5:2"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="5:2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
as預(yù)覽圖
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,488評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,034評(píng)論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,327評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,554評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,337評(píng)論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,883評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,975評(píng)論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,114評(píng)論 0 286
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,625評(píng)論 1 332
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,555評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,737評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,244評(píng)論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 43,973評(píng)論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,615評(píng)論 1 280
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,343評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,699評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容