ConstraintLayout

約束類型:

- Relative positioning(相對(duì)定位)

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

可在橫/豎軸方向添加相對(duì)約束

  • 橫軸: left, right, start and end sides
  • 豎軸: top, bottom sides and text baseline
    相對(duì)位置約束示
    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間距缰雇,指定約束目標(biāo)可見性為Gone時(shí)的間距
  • layout_goneMarginStart
  • layout_goneMarginXXX

- Centering positioning(居中定位)

1.約束居中


設(shè)置具體大小或者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" />
設(shè)置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有三種設(shè)置方式:
  • 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設(shè)置WRAP_CONTENT模式下最大/最小尺寸
layout_constraintWidth_min設(shè)置MATCH_CONSTRAINT模式下最大/最小尺寸

2.Percent dimension

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

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

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(約束鏈)

約束鏈提供單軸向內(nèi)的分組行為


約束鏈以及鏈頭

在鏈頭(最左或最上節(jié)點(diǎn))設(shè)置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

用來(lái)輔助定位巩螃,在顯示時(shí)不可見

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

輔助線的layout_width、layout_height無(wú)意義,layout_constraintGuide_begin和layout_constraintGuide_end同時(shí)存在以后者為準(zhǔn)

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的可見性會(huì)被應(yīng)用到app:constraint_referenced_ids屬性鏈接的所有元素

- Placeholder(占位符)

Placeholder顧名思義期揪,就是用來(lái)一個(gè)占位的東西巾兆,它可以把自己的內(nèi)容設(shè)置為ConstraintLayout內(nèi)的其它view啃奴。因此它用來(lái)寫布局的模版梢褐,也可以用來(lái)動(dòng)態(tài)修改UI的內(nèi)容

其他

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末挑社,一起剝皮案震驚了整個(gè)濱河市庙洼,隨后出現(xiàn)的幾起案子顿痪,更是在濱河造成了極大的恐慌镊辕,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蚁袭,死亡現(xiàn)場(chǎng)離奇詭異征懈,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)揩悄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門卖哎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人删性,你說(shuō)我怎么就攤上這事亏娜。” “怎么了蹬挺?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵维贺,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我巴帮,道長(zhǎng)溯泣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任榕茧,我火速辦了婚禮垃沦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘用押。我一直安慰自己栏尚,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布只恨。 她就那樣靜靜地躺著,像睡著了一般抬虽。 火紅的嫁衣襯著肌膚如雪官觅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天阐污,我揣著相機(jī)與錄音休涤,去河邊找鬼。 笑死笛辟,一個(gè)胖子當(dāng)著我的面吹牛功氨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播手幢,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼捷凄,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了围来?” 一聲冷哼從身側(cè)響起跺涤,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤匈睁,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后桶错,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體航唆,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年院刁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了糯钙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡退腥,死狀恐怖任岸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情阅虫,我是刑警寧澤演闭,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站颓帝,受9級(jí)特大地震影響米碰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜购城,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一吕座、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瘪板,春花似錦吴趴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至兰英,卻和暖如春撇叁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背畦贸。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工陨闹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人薄坏。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓趋厉,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親胶坠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子君账,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

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