ConstraintLayout - 屬性篇

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way.
簡言:ConstraintLayout是可以靈活設(shè)置其內(nèi)控件位置和大小的ViewGroup

支持API 9 (Gingerbread)及以上,官方后續(xù)仍將對其API和功能進行擴展。

添加

1、 Android Studio版本至少2.3
2、 在build.gradle中添加依賴,如下:

dependencies {
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
}

約束類型

當前支持(2017.3)
1、 Relative positioning
2、 Margins
3、 Centering positioning
4、 Visibility behavior
5、 Dimension constraints
6、 Chains
7、 Virtual Helpers objects

注意:在約束條件下不能有循環(huán)依賴關(guān)系。
詳看ConstraintLayout.LayoutParams布局屬性。

開發(fā)者指南

相對位置 Relative positioning

作用:設(shè)置控件與另一個控件的相對位置。

可在水平軸與垂直軸約束控件:

  • 水平軸:Left,、Right,、Start、End
  • 垂直軸:top、bottom、text baseline
Relative Positioning Constraints
序號 屬性
1 layout_constraintLeft_toLeftOf
2 layout_constraintLeft_toRightOf
3 layout_constraintRight_toLeftOf
4 layout_constraintRight_toRightOf
5 layout_constraintTop_toTopOf
6 layout_constraintTop_toBottomOf
7 layout_constraintBottom_toTopOf
8 layout_constraintBottom_toBottomOf
9 layout_constraintBaseline_toBaselineOf
10 layout_constraintStart_toEndOf
11 layout_constraintStart_toStartOf
12 layout_constraintEnd_toStartOf
13 layout_constraintEnd_toEndOf

以上屬性需要另一個控件的idparent(父容器)作為參考:

<Button 
    android:id="@+id/buttonB" ...
    app:layout_constraintLeft_toLeftOf="parent" />

示例:將按鈕B放置到按鈕A的右邊

Relative Positioning Example
<Button android:id="@+id/buttonA" ... />

<Button 
    android:id="@+id/buttonB" ...
    app:layout_constraintLeft_toRightOf="@+id/buttonA" />

邊距 Margins

作用:設(shè)置target控件與source控件的邊距。

示例:將按鈕B放置到按鈕A右邊,并設(shè)置邊距。B:source控件;A:target控件。

Relative Positioning Margins

序號 屬性
1 android:layout_marginStart
2 android:layout_marginEnd
3 android:layout_marginLeft
4 android:layout_marginTop
5 android:layout_marginRight
6 android:layout_marginBottom

注意:邊距只能設(shè)置精確的值(包括0)和尺寸引用。
當位置約束target控件View.GONE時,可用以下margin屬性指示不同的邊距值:

序號 屬性
1 layout_goneMarginStart
2 layout_goneMarginEnd
3 layout_goneMarginLeft
4 layout_goneMarginTop
5 layout_goneMarginRight
6 layout_goneMarginBottom

居中設(shè)置 Centering positioning

ConstraintLayout的優(yōu)勢是如何處理不可能的約束。

<android.support.constraint.ConstraintLayout ...>

    <Button 
        android:id="@+id/button" ...
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</>

以上示例,除非ConstraintLayoutButton的大小完全相同,否則兩個約束不會同時滿足。

Centering Positioning

這種情況下,約束的作用類似于相反的力牽引控件并均分邊距。而控件最終會在父容器中處于居中位置。垂直約束雷同。

偏置 Bias

默認情況下,這種對立的約束會促使控件處于居中位置。此時,可以使用Bias屬性來調(diào)整位置,以達到兩側(cè)邊距不同的效果。

序號 屬性
1 layout_constraintHorizontal_bias
2 layout_constraintVertical_bias

示例:設(shè)置左側(cè)邊距為30%代替默認的50%,如圖:

Centering Positioning with 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/>
</>

可見性操作 Visibility behavior

ConstraintLayout有特定的方式處理被設(shè)置為View.GONE的控件。
通常,設(shè)置GONE的控件不會被顯示,也不是布局本身的一部分(雖然標記為GONE,但實際尺寸不會改變)。
但是,就布局計算而言,GONE的控件仍是布局的一部分,區(qū)別在于:

  • 對于layout pass,尺寸將被看做是0(基本上被處理成了一個點);
  • 如果對其他控件有約束,則仍然會被注重,但是所有邊距值都將看似為0。
Visibility Behavior

這種特定的行為允許在可以設(shè)置控件暫時為GONE的地方構(gòu)建布局,并且不影響布局,這對制作簡單的布局動畫也很有用。

注意:上圖使用的邊距是由B定義的到A的距離。有些情況下這可能不是想要設(shè)置的邊距(例:A到父容器一邊的邊距100dp,BA邊距16dp,此時設(shè)置AGONE,那么B到父容器邊距只有16dp)。出于以上原因,當一個作為參考的控件設(shè)置為GONE時,可以使用替代的邊距值(詳看Margins部分)。

尺寸約束 Dimension constraints

ConstraintLayout 最小尺寸

可以自定義ConstraintLayout的最小尺寸。

  • android:minWidth布局的最小寬度
  • android:minHeight布局的最小高度

ContraintLayout尺寸設(shè)置為WRAP_CONTENT時,將使用最小尺寸。

控件尺寸約束

控件尺寸可以通過三種方式設(shè)置android:layout_widthandroid:layout_height屬性:

  • 使用指定的尺寸(如:123dp、尺寸引用dimen);
  • 使用WRAP_CONTENT,將要求控件自己計算自己的尺寸;
  • 使用0dp,相當于MATCH_CONSTRAINT
Dimension Constraints

前兩個類似。最后一個將調(diào)整控件,以一致的約束來設(shè)置(a:wrap_content;b:0dp)。如果設(shè)置了邊距,布局計算中將會計算到(c:0dp)。

注意:ContraintLayout中的控件不支持MATCH_PARENT,雖然類似的操作可以使用MATCH_CONSTRAINT與設(shè)置相應的left/righttop/bottomparent約束

比例 Ratio

可以定義一個控件相對于另一個控件的尺寸比例。前提是,需要至少設(shè)置一個約束的尺寸為0dp(即MATCH_CONSTRAINT),并通過layout_constraintDimentionRatio屬性設(shè)置比例。

示例:設(shè)置Button的高度與寬度相同

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

比例值設(shè)置方式:

  • float浮點值:表示寬高間的比例
  • width:height方式表示比例

如果寬高都設(shè)置為MATCH_CONSTRAINT (0dp),也可以使用比例Ratio。這種情況下,系統(tǒng)會設(shè)置最大的那個尺寸來滿足所有約束,并保持指定的寬高比。根據(jù)一個有尺寸的控件來約束一個指定約束面的控件。可以預先添加WH來分別約束寬高。例:一個尺寸被兩個條件約束(如:寬度0dp并在父容器中居中),那么可以通過在比例Ratio前添加WH,并用,分隔來標識哪一面應該被約束:

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

高度按照16:9區(qū)分,寬度相對父容器按照相同的約束區(qū)分。

鏈 Chains

作用:在單一軸(水平或垂直)上提供群組行為。另一軸可以獨立約束。

創(chuàng)建鏈

Creating a chain
如果一組控件通過雙向連接連接在一起,那么這組控件就被認為是一個鏈。下圖顯示了一個最小的鏈。

Chain
鏈頭

Chain heads
鏈由鏈的第一個元素(鏈頭)上的屬性控制:

  • 水平鏈鏈頭:最左側(cè)的控件
  • 垂直鏈鏈頭:最上方的控件
Chain Head
鏈中的邊距

Margins in chains
連接中設(shè)置的邊距也會計算在內(nèi)。在鏈展開的情況下,邊距會從分配的空間中扣除。

鏈樣式

Chain Style
當鏈的第一個元素設(shè)置了layout_constraintHorizontal_chainStylelayout_constraintVertical_chainStyle屬性,鏈樣式將按照指定的方式改變(默認是CHAIN_SPREAD)。

  • CHAIN_SPREAD:元素將展開(默認);
  • 權(quán)重鏈:CHAIN_SPREAD模式下,如果一些控件設(shè)置了MATCH_CONSTRAINT,這些控件將分擔可用空間;
  • CHAIN_SPREAD_INSIDE:元素展開,但鏈的端點不會展開;
  • CHAIN_PACKED:鏈中的元素將包裹在一起。子控件的水平或垂直方向的偏置bias屬性會影響包裹中元素的位置。
Paste_Image.png
權(quán)重鏈

Weighted chains
鏈的作用是在可用空間內(nèi)均勻的分布元素。如果一個或多個使用MATCH_CONSTRAINT,它們將搶占可用空間(它們之間均分)。屬性layout_constraintHorizontal_weightlayout_constraintVertical_weight可以控制使用MATCH_CONSTRAINT的元素如何分配空間。例:一條鏈控制了兩個使用MATCH_CONSTRAINT的元素,第一個元素權(quán)重為2,第二個元素權(quán)重為1,那么第一個元素占用的空間是第二個元素的兩倍。

輔助類 Virtual Helpers objects

除了設(shè)置詳細的內(nèi)置屬性,也可以在ConstraintLayout中使用特殊的輔助類來幫助布局。目前,Guideline類創(chuàng)建的水平和垂直的參考,允許ConstraintLayout中的控件作為參考相對定位。參考標準將約束控件的布局。

Guideline

public class Guideline extends View

對于ConstraintLayoutGuideline是一個實用的輔助類。輔助類不會在設(shè)備上顯示(設(shè)置為GONE),并且只用于布局意圖。只能在ConstraintLayout中使用。

Guideline可以是水平的或垂直的:

  • 垂直Guideline:寬度為0,高度為父容器constraintlayout高度;
  • 水平Guideline:高度為0,寬度為父容器constraintlayout寬度。

設(shè)置Guideline有三種較為合理的方式:

  • layout_constraintGuide_begin:從布局的左側(cè)或頂部指定距離
  • layout_constraintGuide_end:從布局的右側(cè)或底部指定距離
  • layout_constraintGuide_percent:指定一個布局的寬高比

然后,控件可以以Guideline為參考設(shè)置約束,允許多個控件以一個Guideline為參考,或者可以使用百分比設(shè)置自適應布局。

XML中設(shè)置Guideline,詳看 ConstraintLayout.LayoutParams 的屬性,以及 ConstraintSet 中相應的 [setGuidelineBegin(int, int)](https://developer.android.google.cn/reference/android/support/constraint/ConstraintSet.html#setGuidelineBegin(int, int))、[setGuidelineEnd(int, int)](https://developer.android.google.cn/reference/android/support/constraint/ConstraintSet.html#setGuidelineEnd(int, int))、[setGuidelinePercent(int, float)](https://developer.android.google.cn/reference/android/support/constraint/ConstraintSet.html#setGuidelinePercent(int, float)) 方法。

示例:垂直Guideline約束按鈕

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

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

</android.support.constraint.ConstraintLayout>

出處

Guideline
ConstraintLayout
ConstraintLayoutExamples

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

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