ConstraintLayout

約束類型:

- Relative positioning(相對定位)

B在A的右邊
<Button android:id="@+id/buttonA" ... />
         <Button android:id="@+id/buttonB" ...
               app:layout_constraintLeft_toRightOf="@+id/buttonA" />

可在橫/豎軸方向添加相對約束

  • 橫軸: left, right, start and end sides
  • 豎軸: top, bottom sides and text baseline
    相對位置約束示
    layout_constraintX_toYOf
    X和Y必須在同一軸向
<Button android:id="@+id/buttonB" ...
                 app:layout_constraintLeft_toLeftOf="parent" />

- Margins

  1. 通用間距
  • android:layout_marginStart
  • android:layout_marginXXX
  1. Gone間距,指定約束目標可見性為Gone時的間距
  • layout_goneMarginStart
  • layout_goneMarginXXX

- Centering positioning(居中定位)

1.約束居中


設置具體大小或者wrap_content
<ImageView
            android:id="@+id/imageView"
            android:layout_width="200dp"
            android:layout_height="200dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round" />
設置0dp或者match_constraint
<ImageView
            android:id="@+id/imageView"
            android:layout_width="0dp"
            android:layout_height="200dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round" />

2.Bias(傾向居中)

  • layout_constraintHorizontal_bias
  • layout_constraintVertical_bias
Bias
<android.support.constraint.ConstraintLayout ...>
             <Button android:id="@+id/button" ...
                 app:layout_constraintHorizontal_bias="0.3"
                 app:layout_constraintLeft_toLeftOf="parent"
                 app:layout_constraintRight_toRightOf="parent/>
         </>

- Circular positioning(圓弧定位)

  • layout_constraintCircle : references another widget id
  • layout_constraintCircleRadius : the distance to the other
    widget center
  • layout_constraintCircleAngle : which angle the widget should be at (in degrees, from 0 to 360)


    圓弧定位
<Button android:id="@+id/buttonA" ... />
  <Button android:id="@+id/buttonB" ...
      app:layout_constraintCircle="@+id/buttonA"
      app:layout_constraintCircleRadius="100dp"
      app:layout_constraintCircleAngle="45" />

- Dimension constraints(尺寸約束)

1.android:layout_width和android:layout_height有三種設置方式:
  • Using a specific dimension (either a literal value such as 123dp or a Dimension reference)
  • Using WRAP_CONTENT, which will ask the widget to compute its own size
  • Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"

android:minWidth設置WRAP_CONTENT模式下最大/最小尺寸
layout_constraintWidth_min設置MATCH_CONSTRAINT模式下最大/最小尺寸

2.Percent dimension

使用百分比大小需要以下步驟:

  • 設置大小屬性為MATCH_CONSTRAINT (0dp)
  • 指定percent方式app:layout_constraintWidth_default="percent" or app:layout_constraintHeight_default="percent"
3.Ratio(長寬比例)

app:layout_constraintDimensionRatio="width:height"

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

顯示指定已知的一邊"H,16:9"

<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控制鏈組樣式

  • CHAIN_SPREAD(均分) -- the elements will be spread out (default style)
  • Weighted chain -- in CHAIN_SPREAD mode, if some widgets are set to MATCH_CONSTRAINT, they will split the available space
  • CHAIN_SPREAD_INSIDE(中間均分) -- similar, but the endpoints of the chain will not be spread out
  • CHAIN_PACKED -- the elements of the chain will be packed together. The horizontal or vertical bias attribute of the child will then affect the positioning of the packed elements
約束鏈

- Virtual Helpers objects

用來輔助定位,在顯示時不可見

- Guideline輔助線
  • 指定到上/左的距離(layout_constraintGuide_begin)
  • 指定到下/右的距離(layout_constraintGuide_end)
  • 指定到上/左的距離百分比(layout_constraintGuide_percent)

輔助線的layout_width、layout_height無意義,layout_constraintGuide_begin和layout_constraintGuide_end同時存在以后者為準

Guideline
<android.support.constraint.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guideline"
            app:layout_constraintGuide_begin="100dp"
            android:orientation="vertical"/>

    <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            app:layout_constraintLeft_toLeftOf="@+id/guideline"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent" />
- Barrier(柵欄)
Barrier
<android.support.constraint.ConstraintLayout...>
  <TextView
    android:id=”@+id/text1" ... />
  <TextView
    android:id=”@+id/text2" ... />
  <android.support.constraint.Barrier
    android:id=”@+id/barrier”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    app:barrierDirection=”end” <!-- start, top, bottom, right... -->
    app:constraint_referenced_ids=”text1,text2" />
  <TextView
    android:id=”@+id/text3"
    ...
    app:layout_constraintStart_toEndOf=”@+id/barrier” />
</android.support.constraint.ConstraintLayout>
- Group
 <android.support.constraint.Group
              android:id="@+id/group"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:visibility="visible"
              app:constraint_referenced_ids="button4,button9" />

Group的可見性會被應用到app:constraint_referenced_ids屬性鏈接的所有元素

- Placeholder(占位符)

Placeholder顧名思義,就是用來一個占位的東西,它可以把自己的內容設置為ConstraintLayout內的其它view。因此它用來寫布局的模版,也可以用來動態修改UI的內容

其他

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容