ConstraintLayout使用

  • 引言android

    ConstraintLayout是一個ViewGroup,容許您以靈活的方式定位和調整小部件的方法,項目中的佈局嵌套問題對項目性能有着不小的威脅,佈局能實現扁平化的話會讓軟件性能獲得很大的提高,而ConstraintLayout就是爲了解決佈局嵌套問題,提示項目的性能。官文有詳細對比ConstraintLayout的性能優點。
    使用方式:
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  • Relative positioning(相對定位)app

    ConstraintLayout 最基本的屬性控制有如下幾個,即 layout_constraintXXX_toYYYOf 格式的屬性,即將「View A」的方向 XXX 置於 「View B」的方向 YYY 。View B 能夠是父容器即 ConstraintLayout ,用「parent」來表示。
    • layout_constraintLeft_toLeftOf:View A 與 View B 左對齊
    • layout_constraintLeft_toRightOf:View A左邊置於View B的右邊
    • layout_constraintTop_toTopOf:View A頂部與View B頂部對齊
    • layout_constraintBaseline_toBaselineOf:View A 內部文字與 View B 內部文字對齊
    • layout_constraintRight_toLeftOf:如下相似
    • layout_constraintRight_toRightOf
    • layout_constraintTop_toBottomOf
    • layout_constraintBottom_toTopOf
    • layout_constraintBottom_toBottomOf
    • layout_constraintStart_toEndOf
    • layout_constraintStart_toStartOf
    • layout_constraintEnd_toStartOf
    • layout_constraintEnd_toEndOf

    例如:TextView在Button的底部對齊,若是想設置兩個View的間距,可使用android:layout_marginXXX來設置,但其中一個View是Gone的,還須要保存間距可使用app:layout_goneMarginXXX來設置。佈局

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="40dp"
            app:layout_constraintTop_toBottomOf="@+id/button"/>
    <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
  • Bias(偏壓)性能

    相反的約束時的默認設置是使窗口小部件居中; 可是您可使用誤差屬性調整定位以使一側偏向另外一側。
    * layout_constraintHorizontal_bias:橫向偏壓
    * layout_constraintVertical_bias:豎向偏壓優化

    例如,想把Button向右橫向偏壓0.6code

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="40dp"
            app:layout_constraintTop_toBottomOf="@+id/button"/>
    <Button
            android:text="@string/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintHorizontal_bias="0.6"/>
  • Circular positioning(循環定位)blog

    以角度和距離約束窗口小部件中心相對於另外一個窗口小部件中心,這容許您將小部件放在圓上,可使用如下屬性。
    * layout_constraintCircle :引用另外一個小部件ID
    * layout_constraintCircleRadius :到其餘小部件中心的距離
    * layout_constraintCircleAngle :小部件應該處於哪一個角度(以度爲單位,從0到360)get

    <Button
            android:text="@string/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="84dp"
            app:layout_constraintHorizontal_bias="0.547"/>
    
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintCircle="@+id/button"
            app:layout_constraintCircleRadius="60dp"
            app:layout_constraintCircleAngle="45"/>
  • Constraints(約束)string

    將維度設置爲WRAP_CONTENT,在1.1以前的版本中,它們將被視爲文字維度 - 這意味着約束不會限制生成的維度。雖然一般這足夠,但在某些狀況下,您可能但願使用WRAP_CONTENT,但仍然強制執行約束以限制生成的維度。在這種狀況下,您能夠添加一個相應的屬性。
    * app:layout_constrainedWidth=」true|false」
    * app:layout_constrainedHeight=」true|false」it

    當維度設置爲時MATCH_CONSTRAINT,默認行爲是使結果大小佔用全部可用空間,可使用如下屬性。
    * layout_constraintWidth_min和layout_constraintHeight_min:將設置此維度的最小大小
    * layout_constraintWidth_max和layout_constraintHeight_max:將設置此維度的最大大小
    * layout_constraintWidth_percent和layout_constraintHeight_percent:將此維度的大小設置爲父級的百分比

    要使用百分比,您須要設置如下內容:
    * 尺寸應設置爲MATCH_CONSTRAINT(0dp)
    * 默認值應設置爲百分比app:layout_constraintWidth_default="percent" 或app:layout_constraintHeight_default="percent" 
    * 而後將layout_constraintWidth_percent 或layout_constraintHeight_percent屬性設置爲0到1之間的值

  • Ratio(比)

    能夠將view的一個維度定義爲另外一個維度的比率。爲此,您須要將至少一個約束維度設置爲0dp(即MATCH_CONSTRAINT),並將該屬性layout_constraintDimensionRatio設置爲給定比率。例如

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

    兩個尺寸都設置爲MATCH_CONSTRAINT(0dp),您也可使用比率,系統設置知足全部約束的最大尺寸並保持指定的縱橫比。要根據另外一個特定邊的尺寸限制一個特定邊,能夠預先附加W,「或」 H,分別約束寬度或高度。例如

    <Button android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="H,16:9"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
  • Chains(鏈)

    設置屬性layout_constraintHorizontal_chainStyle或layout_constraintVertical_chainStyle鏈的第一個元素時,鏈的行爲將根據指定的樣式更改(默認值CHAIN_SPREAD)。
    • CHAIN_SPREAD - 元素將展開(默認樣式)
    • 加權連接CHAIN_SPREAD模式,若是設置了一些小部件MATCH_CONSTRAINT,它們將分割可用空間
    • CHAIN_SPREAD_INSIDE - 相似,但鏈的端點不會分散
    • CHAIN_PACKED - 鏈條的元素將被包裝在一塊兒。而後,子項的水平或垂直誤差屬性將影響打包元素的定位
  • Optimizer(優化器)

    經過標記app:layout_optimizationLevel添加到ConstraintLayout元素來決定應用哪些優化。 * none : 未應用任何優化 * standard : 默認。僅優化直接和障礙約束 * direct : 優化直接約束 * barrier : 優化障礙限制 * chain : 優化鏈約束 * dimensions : 優化維度度量,減小匹配約束元素的度量數量

相關文章
相關標籤/搜索