ConstraintLayout 從 0「入門」 到 1 「放棄」

  1. Dimensions constraints 「尺寸約束」

1.1 Minimum dimensions on ConstraintLayout
可以使用 android:minWidth 與 android:minHeight 。
當(dāng) ConstraintLayout 的尺寸被設(shè)置為 WRAP_CONTENT 時(shí),這兩個(gè)屬性會(huì)起作用。

1.2 Widgets dimension constraints
與其他布局的區(qū)別是沒有「MATCH_PARENT」,可以使用 「0dp」代替。

 <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">

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="32dp"
            android:background="@color/c_FAB256"
            android:text="MinSuFragment"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
image.png

1.3 Ratio
利用「layout_constraintDimensionRatio」這個(gè)屬性可以根據(jù)比例去設(shè)置view 的寬或者高。

第一種用法:

    <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">
      <!--
        解釋下這個(gè)布局
        首先是高度 在 layout_constraintBottom_toBottomOf
         layout_constraintTop_toTopOf  layout_height="0dp"
         三個(gè)屬性值的設(shè)置下,高度是根布局的高度;
         然后,設(shè)置寬度是 wrap_content ;
         最后,設(shè)置屬性 layout_constraintDimensionRatio="1:1"  ,
         則,此 View 的高度就是 View 的寬度值。即,根據(jù)寬度去改變高度。
        -->
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@color/c_FAB256"
            app:layout_constraintDimensionRatio="1:1"
            android:text="MinSuFragment" />
    </android.support.constraint.ConstraintLayout>
image.png

第二種用法:

  <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">

        <!--
            解釋下這個(gè)的使用:
            當(dāng) view 的寬高均設(shè)置為如下屬性,
            再添加屬性 layout_constraintDimensionRatio="H,2:1"
            則表示,高度調(diào)整為屏幕寬度的二分之一「比例始終都是 寬:高 」
        -->

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintDimensionRatio="H,2:1"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@color/c_FAB256"
            android:text="MinSuFragment" />
    </android.support.constraint.ConstraintLayout>
image.png

把上面的 app:layout_constraintDimensionRatio="W,1:2" 后,則表示動(dòng)態(tài)改變寬度,依然是 寬:高 = 1:2 。

image.png

待繼續(xù) .......

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容