天天看點

關于ConstraintLayout的部分屬性總結(強化版RelativeLayout,包含LinearLayout的比例屬性,減少層級利器)

一、使用方式:

Android studio 2.2以上

并添加依賴

compile ‘com.android.support.constraint:constraint-layout:1.0.1’

二、相對位置屬性如下:

  • layout_constraintLeft_toLeftOf :目前View的左側和另一個View的左側位置對齊,與RelativeLayout的alignLeft屬性相似
  • layout_constraintLeft_toRightOf :目前view的左側會在另一個View的右側位置 與RelativeLayout的toRightOf屬性相似
  • layout_constraintRight_toLeftOf :目前view的右側會在另一個View的左側位置 與RelativeLayout的toLeftOf屬性相似
  • layout_constraintRight_toRightOf :目前View的右側和另一個View的右側位置對齊,與RelativeLayout的alignRight屬性相似
  • layout_constraintTop_toTopOf :頭部對齊,與alignTop相似
  • layout_constraintTop_toBottomOf :目前View在另一個View的下側 與below相似
  • layout_constraintBottom_toTopOf :目前View在另一個View的上方 與above相似
  • layout_constraintBottom_toBottomOf :底部對齊,與alignBottom屬性相似
  • layout_constraintBaseline_toBaselineOf :文字底部對齊,與alignBaseLine屬性相似
  • layout_constraintStart_toEndOf :同left_toRightOf
  • layout_constraintStart_toStartOf :同left_toLeftOf
  • layout_constraintEnd_toStartOf :同right_toLeftOf
  • layout_constraintEnd_toEndOf :同right_toRightOf

三、Margins屬性:同RelativeLayout屬性

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

四、Margins when connected to a Gone widget

目前View與另一個View綁定後,另一個View的屬性設定為了Gone,則以下屬性會生效

  • layout_goneMarginStart
  • layout_goneMarginEnd
  • layout_goneMarginLeft
  • layout_goneMarginTop
  • layout_goneMarginRight
  • layout_goneMarginBottom

五、center position and bias

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

設定方法:以橫向居中為例:

将ConstraintLayout的子View的屬性如下進行設定

<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"
        app:layout_constraintTop_toTopOf="parent" />
           

将寬度設定為wrap_content并設定兩個屬性:

app:layout_constraintLeft_toLeftOf="parent"
  app:layout_constraintRight_toRightOf="parent"
           

即可将該View橫向居中顯示。(豎向同理)。

利用bias設定權重:

設定居中之後預設的權重的為0.5。即正中央顯示,效果如圖
關于ConstraintLayout的部分屬性總結(強化版RelativeLayout,包含LinearLayout的比例屬性,減少層級利器)

可以通過設定app:layout_constraintHorizontal_bias屬性進行位置的調整,表示距離左側(因為是橫向,縱向則是距離頂部)30%的的距離。

六、Dimension constraints

  • android:minWidth set the minimum width for the layout
  • android:minHeight set the minimum height for the layout

當子View的寬/高設定為wrap_content時,會用到minWidth和minHeight這兩個屬性

七、Widget dimension constraints

  • //使用一個具體的值

    Using a specific dimension (either a literal value such as 123dp or a Dimension reference)

  • //使用wrap_content讓控件自己來計算大小

    Using WRAP_CONTENT, which will ask the widget to compute its own size

  • //用0dp來指定,意思就是Match_Constraint

    Using 0dp, which is the equivalent of “MATCH_CONSTRAINT”

注意:

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

八、Ratio比例大小屬性

當你的父控件為ConstraintLayout,可以利用這個屬性來控制目前View的寬高比。在利用這個屬性時,你必須指明一個方向上的大小為0dp,另一個方向可指定為明确的dp值也可以使用wrap_content這樣才能夠按照比例來為你擺放
           
<ImageView
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintDimensionRatio="1:4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
           
通過指定 app:layout_constraintDimensionRatio="1:1" 屬性來指定控件寬高的比,預設為寬:高
           

你也可以通過下面的方法進行設定:

<ImageView
 android:layout_width="0dp"
 android:layout_height="0dp"
 android:src="@mipmap/ic_launcher"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintDimensionRatio="H,3:1"
 app:layout_constraintTop_toTopOf="parent" />
           
上面的這種情況将寬和高均指定為了0dp,但是通過top_toTopOf bottom_toBottomOf 兩個屬性指定了在高度上的大小。另外通過在ratio屬性中指明的H,是為了告訴ConstraintLayout已經指定了高度上的大小。而并非是指定比例為高:寬
           

九、Chains

chains:鎖鍊,通過設定屬性可以将一個方向上的控件形成鎖鍊(互相依賴),并且能夠實作比例分布(類似于LinearLayout的weight分布)
           

形成chains的條件:

  • head(即chains的第一個view)必須包含top_toTopOf或者left_toLeftOf
    • chain的最後一個View必須指定bottom_toBottomOf或者right_toRightOf
    • chain中的View必須互相依賴
如:view a 和view b在豎直方向上形成鎖鍊:
    a的屬性設定為 top_toTopOf = “parent” bottom_toTopOf = “b” b的屬性設定為top_toBottomOf=“a” bottom_toBottom = “parent"
           

形成依賴的好處:可以使用比例進行設定(使用方式同LinearLayout的weight屬性,效果也相同)

chains Style(chains 樣式)

提供了三種預設的樣式:spread、packed、spread_inside

spread樣式:

關于ConstraintLayout的部分屬性總結(強化版RelativeLayout,包含LinearLayout的比例屬性,減少層級利器)

spread_inside樣式:

關于ConstraintLayout的部分屬性總結(強化版RelativeLayout,包含LinearLayout的比例屬性,減少層級利器)

包含weight屬性的chain

關于ConstraintLayout的部分屬性總結(強化版RelativeLayout,包含LinearLayout的比例屬性,減少層級利器)

packed樣式

關于ConstraintLayout的部分屬性總結(強化版RelativeLayout,包含LinearLayout的比例屬性,減少層級利器)

有bias的packed樣式

關于ConstraintLayout的部分屬性總結(強化版RelativeLayout,包含LinearLayout的比例屬性,減少層級利器)

十、ConstraintSet類:

這個類是輔助進行代碼中動态設定ConstraintSet屬性的。
           

具體文檔參見官方API

十一、GuideLine類:

官方文檔:Widgets can then be constrained to a Guideline, allowing multiple widgets to be positioned easily from one Guideline, or allowing reactive layout behavior by using percent positioning.

GuideLine類是一個輔助的導航線類,預設是不顯示的,隻提供一個占位符以供其他View進行限制。(可通過android:orientation指定線的方向)
           

GuideLine一共有3種指定的方式:

specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin) //在距離頂部或者左側一定距離的地方放置一條導航線

specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end) //在距離右側或者底部一定距離的地方放置一條導航線

specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)//按照父視圖的百分比放置一條導航線

tips:雖然我很菜,但是我必須說一下,如果有購買ssr需求的小夥伴,推薦一下我的連結,速度很快比較穩定,請自行選擇

https://staryun.me/auth/register?code=AfxUstxSiXYzO7XeDVh7gpA9wghxo3vs