ConstraintLayout常用且好用的特性總結(jié)

  1. 寬高比
    當(dāng)ConstraintLayout布局中某個(gè)控件的寬或高中的一個(gè)被嚴(yán)格約束(所謂嚴(yán)格約束即指大小可被推斷)則另一個(gè)可通過layout_constraintDimensionRatio屬性來設(shè)置大小為與另一邊有指定的比率臭胜。屬性的值即為寬與高的比例值勾扭,可以是一個(gè)浮點(diǎn)數(shù)序仙,也可以是x:y的形式〉暾梗可以通過在比例值前面添加'w'或者‘h’并用逗號(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"/>

以上代碼實(shí)現(xiàn)了按鈕的高度與寬度的比為16:9,寬度與父控件同寬

  1. 按百分比定義控件的寬或高
    按照百分比定義控件的寬高步驟如下:
    • 將布局中某個(gè)控件的寬或者高設(shè)置為0或者M(jìn)ATCH_CONSTRAINT (0dp)
    • 設(shè)置app:layout_constraintWidth_default="percent"
    • 設(shè)置layout_constraintWidth_percent 或layout_constraintHeight_percent屬性的值為(0医吊,1]之間的某個(gè)小數(shù)

  2. ConstraintLayout本身就是基于依賴的布局,當(dāng)幾個(gè)控件之間在水平方向或垂直方向相互依賴時(shí)逮京,就形成了鏈卿堂。如下圖所示:


    包含3個(gè)元素的鏈

    實(shí)現(xiàn)上圖所示布局的代碼如下:

 <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/TextView2" />

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView1"
        app:layout_constraintRight_toLeftOf="@+id/TextView3"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView2"

鏈頭及樣式
鏈有鏈頭,水平鏈的最左端元素或者垂直鏈的最上端元素成為鏈頭懒棉。在鏈頭中可以通過layout_constraintHorizontal_chainStyle屬性定義鏈的樣式(Style)草描。chains提供了3種樣式,分別是:

  • CHAIN_SPREAD —— 展開元素 (默認(rèn))策严;
  • CHAIN_SPREAD_INSIDE —— 展開元素穗慕,但鏈的兩端貼近parent;
  • CHAIN_PACKED —— 鏈的元素將被打包在一起妻导。
    權(quán)重鏈
    可以通過將鏈中某個(gè)元素的寬(水平鏈)或高(垂直鏈)設(shè)置為0或者M(jìn)ATCH_CONSTRAINT逛绵,然后通過layout_constraintHorizontal_weight屬性給控件設(shè)置權(quán)重,權(quán)重越大所占的空間越大倔韭。代碼如下:
<TextView
        android:id="@+id/TextView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/TextView2"
        app:layout_constraintHorizontal_weight="2" />

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView1"
        app:layout_constraintRight_toLeftOf="@+id/TextView3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="3" />

    <TextView
        android:id="@+id/TextView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/TextView2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="4" />

不同樣式的鏈及權(quán)重鏈的示意圖如下:


不同樣式的鏈
  1. 屏障(Barrier)
    假如有這樣一個(gè)需求术浪,有A,B狐肢,C 3個(gè)控件添吗,A,B的寬度不固定,現(xiàn)在要求無論A,B的寬度如何變化控件C始終在A和B的右側(cè)份名。那么就可以通過Barrier輕松實(shí)現(xiàn)碟联。只要通過Barrier將A,B與C控件分割開妓美,讓A,B在Barrier的左側(cè)鲤孵,C在Barrier的右側(cè)即可壶栋,代碼如下:
<TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/TextView1" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="right"
        app:constraint_referenced_ids="TextView1,TextView2" />

    <TextView
        android:id="@+id/TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/barrier" />

實(shí)現(xiàn)的 效果如下圖所示


  1. 分組(Group)
    可以將幾個(gè)控件歸為一組,這樣做的一個(gè)好處是可以很方便的控制這些控件的可見性普监。同一控件可以屬于多個(gè)分組贵试,它的可見性由最后一個(gè)分組決定。代碼如下:
<androidx.constraintlayout.widget.Group
              android:id="@+id/group"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:visibility="visible"
              app:constraint_referenced_ids="button4,button9" />
  1. 輔助線(Guideline)
    Guileline在布局中并不可見凯正,但是它可以為布置其他控件提供約束毙玻,作為其他控件的位置參考。Guideline有水平和垂直之分廊散,通過android:orientation屬性設(shè)置桑滩。水平Guideline高度為0,寬度為它的父控件寬度允睹,垂直Guideline寬度為0畔师,高度為它的父控件高度实苞」世可以通過3個(gè)屬性來確定Guideline在布局中的位置
    • layout_constraintGuide_begin 指定Guideline距父控件左或上的距離
    • layout_constraintGuide_end 指定Guideline距父控件右或下的距離
    • layout_constraintGuide_percent 指定Guideline距父控件左或上的百分比

以下代碼將一個(gè)Button控件約束到一個(gè)垂直Guideline

<androidx.constraintlayout.widget.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">

    <androidx.constraintlayout.widget.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" />

</androidx.constraintlayout.widget.ConstraintLayout>

以上便是ConstraintLayout比較常見且較為有用的特性鹿驼。合理使用這些特性將會(huì)起到事半功倍的效果。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末米者,一起剝皮案震驚了整個(gè)濱河市韭畸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌塘雳,老刑警劉巖陆盘,帶你破解...
    沈念sama閱讀 216,744評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異败明,居然都是意外死亡隘马,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門妻顶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來酸员,“玉大人,你說我怎么就攤上這事讳嘱♂`拢” “怎么了?”我有些...
    開封第一講書人閱讀 163,105評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵沥潭,是天一觀的道長(zhǎng)邀泉。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么汇恤? 我笑而不...
    開封第一講書人閱讀 58,242評(píng)論 1 292
  • 正文 為了忘掉前任庞钢,我火速辦了婚禮,結(jié)果婚禮上因谎,老公的妹妹穿的比我還像新娘基括。我一直安慰自己,他們只是感情好财岔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評(píng)論 6 389
  • 文/花漫 我一把揭開白布风皿。 她就那樣靜靜地躺著,像睡著了一般匠璧。 火紅的嫁衣襯著肌膚如雪桐款。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評(píng)論 1 299
  • 那天患朱,我揣著相機(jī)與錄音鲁僚,去河邊找鬼。 笑死裁厅,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的侨艾。 我是一名探鬼主播执虹,決...
    沈念sama閱讀 40,096評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼唠梨!你這毒婦竟也來了袋励?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,939評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤当叭,失蹤者是張志新(化名)和其女友劉穎茬故,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蚁鳖,經(jīng)...
    沈念sama閱讀 45,354評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡磺芭,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了醉箕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钾腺。...
    茶點(diǎn)故事閱讀 39,745評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖讥裤,靈堂內(nèi)的尸體忽然破棺而出放棒,到底是詐尸還是另有隱情,我是刑警寧澤己英,帶...
    沈念sama閱讀 35,448評(píng)論 5 344
  • 正文 年R本政府宣布间螟,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏厢破。R本人自食惡果不足惜邮府,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望溉奕。 院中可真熱鬧褂傀,春花似錦、人聲如沸加勤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鳄梅。三九已至叠国,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間戴尸,已是汗流浹背粟焊。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留孙蒙,地道東北人项棠。 一個(gè)月前我還...
    沈念sama閱讀 47,776評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像挎峦,于是被迫代替她去往敵國(guó)和親香追。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評(píng)論 2 354

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