ConstraintLayout使用詳解

ConstraintLayout基本使用

Relative positioning

約束 :view 在垂直方向 吝镣,或在水平方向趴腋,相對(duì)于其他view 的關(guān)系(相互的)

在這里插入圖片描述

相關(guān)屬性

layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

示例 :

<View
        android:id="@+id/test"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

Centering and Margins

Margins(偏移)

在有約束的情況下才能有效果
效果:


在這里插入圖片描述

示例 :

<View
        android:id="@+id/test"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>


<View
        android:id="@+id/test1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintLeft_toRightOf="@+id/test"
        android:layout_marginStart="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

margin with gone

當(dāng)與之相關(guān)聯(lián)的view狀態(tài)為GONE時(shí)如果需要可以使用一下屬性設(shè)置margin

layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

效果

在這里插入圖片描述

居中

<Button android:id="@+id/button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent/>
在這里插入圖片描述

垂直居中類似

偏移占比 (Bias)

調(diào)整view 位置蔓彩,不僅可以使用居中省核,margin 等,還可以使用百分比:

layout_constraintHorizontal_bias  
layout_constraintVertical_bias

上面兩個(gè)屬性取值范圍 [0,1] 0 : 最左 或 最上 包晰, 1:最右 或 最下

偷個(gè)例子

 <Button android:id="@+id/button" ...
         app:layout_constraintHorizontal_bias="0.3"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent/>
在這里插入圖片描述

Chains

Creating a chain

<View
        android:id="@+id/test"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/test1"
        app:layout_constraintBottom_toBottomOf="parent"/>


<View
        android:id="@+id/test1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintLeft_toRightOf="@+id/test"
        android:layout_marginStart="10dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

類似效果:


在這里插入圖片描述

Chain Style

在鏈的第一個(gè)元素上設(shè)置屬性layout_constraintHorizontal_chainStyle或layout_constraintVertical_chainStyle時(shí)尝艘,鏈的行為將根據(jù)指定的樣式更改(默認(rèn)為spread)
spread : 所有元素分布開
packed :所有元素在一起
spread_inside : (同spread )首尾元素除外

在這里插入圖片描述

Chain heads

Chain heads 指chain中水平最左演侯,垂直最上的view

Chain margin

上面可以看出,chain 中的左中右的間距都是相同的(平分剩下的空間)背亥,當(dāng)chain 中的view 設(shè)置margin 的時(shí)候會(huì)把除去margin 后剩余空間平分分配 秒际。
效果

在這里插入圖片描述

weightd chain

鏈的默認(rèn)行為是在可用空間中平均分布元素。如果一個(gè)或多個(gè)元素使用MATCH_CONSTRAINT狡汉,它們將使用可用的空白空間(在它們之間平均分配)娄徊。layout_constraintHorizontal_weight和layout_constraintVertical_weight屬性將控制如何使用MATCH_CONSTRAINT在元素之間分配空間。例如盾戴,在使用MATCH_CONSTRAINT的包含兩個(gè)元素的鏈上寄锐,第一個(gè)元素使用權(quán)重2,第二個(gè)元素使用權(quán)重1尖啡,第一個(gè)元素占用的空間將是第二個(gè)元素占用的空間的兩倍橄仆。

例如

<View
        android:id="@+id/button1"
        android:layout_width="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button2"
        app:layout_constraintLeft_toLeftOf="parent"
        android:background="@color/colorPrimary"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_height="100dp"/>

<View
        android:id="@+id/button2"
        android:layout_width="0dp"
        app:layout_constraintLeft_toRightOf="@+id/button1"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorAccent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_height="100dp"/>

Circular positioning (Added in 1.1)

您可以以角度和距離約束控件中心相對(duì)于另一個(gè)控件的中心。這使你可以將控件放在圓上衅斩。
可以使用以下屬性:

layout_constraintCircle : 另一個(gè)控件的ID  
layout_constraintCircleRadius : 相對(duì)另一控件中心的距離
layout_constraintCircleAngle : 位于另一控件的角度位置 

示例

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

在這里插入圖片描述

Note: 0度角為正上方盆顾,向右為增大方向,如果設(shè)置為負(fù)數(shù) (-90 會(huì)按照絕對(duì)值來計(jì)算)

Dimensions constraints(尺寸限制)

ConstraintLayout 自身的尺寸約束

最大最小寬高設(shè)置

android:minWidth 
android:minHeight 
android:maxWidth 
android:maxHeight  

設(shè)置方式類似其他布局 只有在其尺寸設(shè)置為WRAP_CONTENT作用

控件的尺寸約束

固定尺寸 (123dp)

  • WRAP_CONTENT
  • 0dp(MATCH_CONSTRAINT) 相當(dāng)于MATCH_PARENT

**Note **:不建議對(duì)ConstraintLayout中包含的小部件使用MATCH_PARENT畏梆∧埽可以通過使用MATCH_CONSTRAINT來定義類似的行為奈懒,其中相應(yīng)的左/右或上/下約束被設(shè)置為"parent"。 ** (即 :約束邊作為其大小) **

WRAP_CONTENT

enforcing constraints(強(qiáng)制約束)

一般情況下使用WRAP_CONTENT 宪巨,表示在可用剩余空間下任意大小
如果想要在使用WRAP_CONTENT的時(shí)候限制一個(gè)大小范圍使用一下方式:

app:layout_constrainedWidth=”true|false”
app:layout_constrainedHeight=”true|false”

再配合最大最小的最大最小寬度去設(shè)置:

layout_constraintWidth_min 
layout_constraintHeight_min  
layout_constraintWidth_max
layout_constraintHeight_max

MATCH_CONSTRAINT

Percent dimension(百分比尺寸設(shè)置)

要使用百分比磷杏,您需要設(shè)置以下內(nèi)容:

  • 尺寸應(yīng)設(shè)置為MATCH_CONSTRAINT(0dp)
  • 默認(rèn)值應(yīng)設(shè)置為app app:layout_constraintWidth_default =“percent”或app:layout_constraintHeight_default =“percent”
  • 然后將layout_constraintWidth_percent或layout_constraintHeight_percent屬性設(shè)置為0到1之間的值

比率 Ratio

可以將控件的一個(gè)維度定義為另一個(gè)維度的比率。
需要將至少一個(gè)約束維度設(shè)置為0dp(即MATCH_CONSTRAINT)揖铜,并將屬性layout_constraintDimensionRatio設(shè)置為給定比率茴丰。
例如

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

另一種表示方式:

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

浮點(diǎn)值达皿,表示寬度和高度之間的比率

如果兩個(gè)尺寸都設(shè)置為MATCH_CONSTRAINT(0dp)天吓,也可以使用比率。
在這種情況下峦椰,系統(tǒng)設(shè)置最大尺寸以滿足所有約束并保持指定的縱橫比龄寞。要根據(jù)另一個(gè)的尺寸約束一個(gè)特定邊,可以預(yù)先附加W汤功,“或H物邑,分別約束寬度或高度。

例如滔金,如果一個(gè)維度受兩個(gè)目標(biāo)約束(例如色解,寬度為0dp并且以父對(duì)象為中心)你可以通過在比率前添加字母W(用于約束寬度)或H(用于約束高度)來指示哪一邊應(yīng)該被約束,用逗號(hào)分隔:

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

Virtual Helper objects

GuildeLine

作用:輔助布局

設(shè)置guildeLine 方向

**通過 orientation的屬性設(shè)置水平或者垂直 **
水平guildeLine 高度為0 寬度為parent
垂直guildeLine 寬度為0 高度為 parent
eg:

<android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        android:orientation="horizontal"/>

確定guildeLine 位置

三種方式:

  • layout_constraintGuide_begin
  • layout_constraintGuide_end
  • layout_constraintGuide_percent [0,1]

Barrier

屏障

使用場(chǎng)景

參考博客

Placeholder

顧名思義:就是占位圖

使用示例

<android.support.constraint.Placeholder
    android:id="@+id/imageHolder"
    android:layout_width="100dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_height="100dp"/>

<ImageView
    android:id="@+id/image"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_width="100dp"
    android:src="@mipmap/ic_launcher"
    android:layout_height="100dp"/>
image.setOnClickListener { 
    imageHolder.setContentId(R.id.image)
}

Group

作用:主要用來控制一組控件的可見性餐茵。

示例

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
            android:id="@+id/buttonA"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="wrap_content"
            android:text="buttonA"
            android:layout_height="wrap_content"/>

    <Button
            android:id="@+id/buttonB"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="wrap_content"
            android:text="buttonB"
            android:layout_height="wrap_content"/>


    <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="buttonA,buttonB"/>

</android.support.constraint.ConstraintLayout>

Group 的可見性對(duì)應(yīng)著與其關(guān)聯(lián)的幾個(gè)控件科阎,方便的顯示隱藏多個(gè)控件。

注意:ConstraintLayout可用作支持庫忿族,可以在API級(jí)別9(Gingerbread)的Android系統(tǒng)上使用

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末锣笨,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子道批,更是在濱河造成了極大的恐慌错英,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件隆豹,死亡現(xiàn)場(chǎng)離奇詭異椭岩,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)璃赡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門判哥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鉴吹,你說我怎么就攤上這事姨伟。” “怎么了豆励?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵夺荒,是天一觀的道長(zhǎng)瞒渠。 經(jīng)常有香客問我,道長(zhǎng)技扼,這世上最難降的妖魔是什么伍玖? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮剿吻,結(jié)果婚禮上窍箍,老公的妹妹穿的比我還像新娘。我一直安慰自己丽旅,他們只是感情好椰棘,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著榄笙,像睡著了一般邪狞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上茅撞,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天帆卓,我揣著相機(jī)與錄音,去河邊找鬼米丘。 笑死剑令,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的拄查。 我是一名探鬼主播吁津,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼靶累!你這毒婦竟也來了腺毫?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤挣柬,失蹤者是張志新(化名)和其女友劉穎潮酒,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體邪蛔,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡急黎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了侧到。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片勃教。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖匠抗,靈堂內(nèi)的尸體忽然破棺而出故源,到底是詐尸還是另有隱情,我是刑警寧澤汞贸,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布绳军,位于F島的核電站印机,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏门驾。R本人自食惡果不足惜射赛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望奶是。 院中可真熱鬧楣责,春花似錦、人聲如沸聂沙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽逐纬。三九已至蛔屹,卻和暖如春削樊,著一層夾襖步出監(jiān)牢的瞬間豁生,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工漫贞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留甸箱,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓迅脐,卻偏偏與公主長(zhǎng)得像芍殖,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谴蔑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

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

  • 今天讀書會(huì)更加深刻理解了種子法則豌骏,復(fù)習(xí)筆的故事;所有我們看到或聽到美好的或者不好的都是我們的業(yè)力種子所呈現(xiàn)出來的隐锭;...
    雪俊閱讀 347評(píng)論 0 1
  • 現(xiàn)在網(wǎng)絡(luò)中充斥這各式各樣的信息窃躲,那么咱們的計(jì)算機(jī)是如何上網(wǎng)的呢?不知道你是否考慮過這個(gè)問題钦睡?今天就討論下蒂窒,我們是如...
    封不然閱讀 1,047評(píng)論 0 4
  • ‘夢(mèng)里夢(mèng)見醒不來的夢(mèng)……’ 最近迷上張碧晨的這首《紅玫瑰》了 現(xiàn)在學(xué)校又給我們放一星期的假,我在家都不知道干什么荞怒,...
    馬蟻May閱讀 131評(píng)論 0 0
  • 大 年初三蜻底,總在喚起人的記憶骄崩。 如果你沒有記憶,那么薄辅,同學(xué)的電話會(huì)把你從瑣碎的生活事物中換醒要拂。 ...
    紫雨荷閱讀 153評(píng)論 0 1