ConstraintLayout

基礎(chǔ)知識

1. Relative positioning相對定位

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

總結(jié):layout_constraintA_toB 參數(shù)為目標(biāo)id或者parent

A當(dāng)前控件磁玉,B目標(biāo)控件,A控件的哪一側(cè)(上下左右)與B控件的哪一側(cè)(上下左右)對齊

例如layout_constraintLeft_toLeftOf 控件A的左側(cè)和控件B的左側(cè)對齊
例如layout_constraintTop_toBottomOf 控件A的頂部和控件B的底部側(cè)對齊

注意薯鼠,如果當(dāng)前控件的layout_height或者layout_width值為match_parent觉吭,約束會失效寡夹,改為0dp即可

2. Margins

【1】基礎(chǔ)信息

android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
注意,值必須為正數(shù)或者0彼宠,使用Dimension

【2】當(dāng)被依賴控件隱藏View.GONE時浴捆,下面的幾個屬性生效

layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

當(dāng)約束目標(biāo)控件可見性設(shè)為View.GONE時,啟用該屬性烙心,代碼示例如下

正常顯示,圖1
<androidx.constraintlayout.widget.ConstraintLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/mainBkg">
?
   <TextView
   android:id="@+id/view"
   android:layout_width="200dp"
   android:layout_height="200dp"
   android:layout_marginStart="50dp"
   android:layout_marginTop="100dp"
   android:background="#fff"
   android:gravity="center"
   android:text="目標(biāo)控件"
   android:visibility="visible"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent" />
?
   <TextView
   android:layout_width="50dp"
   android:layout_height="50dp"
   android:layout_marginStart="20dp"
   android:layout_marginTop="20dp"
   android:background="#123"
   android:gravity="center"
   android:text="當(dāng)前控件"
   android:textColor="#fff"
   app:layout_constraintStart_toEndOf="@id/view"
   app:layout_constraintTop_toTopOf="@id/view"
   app:layout_goneMarginStart="100dp"
   app:layout_goneMarginTop="200dp" />
  ?
 </androidx.constraintlayout.widget.ConstraintLayout>
圖1
隱藏目標(biāo)控件后android:visibility="gone"
圖2

3. Centering positioning中心定位和偏差

當(dāng)控件左側(cè)與父控件左側(cè)對齊膜廊,右側(cè)與父控件右側(cè)對齊時app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent",除非控件大小和父控件大小一致淫茵,否側(cè)會在約束方向上(此時為橫向)居中顯示爪瓜,同理縱向也是一樣

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="居中"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
居中

偏移量

當(dāng)控件在父控件中居中顯示或某方向居中顯示時,使用bias屬性調(diào)整控件偏移位置
layout_constraintHorizontal_bias layout_constraintVertical_bias匙瘪,方向為從上到下铆铆,從左到右,如果layout_constraintHorizontal_bias設(shè)為1丹喻,位于父控件最右側(cè)薄货,layout_constraintVertical_bias設(shè)為1,位于控件最底部
取值范圍float驻啤,0-1菲驴,如果大于1會顯示不全,默認(rèn)0.5
如圖骑冗,目標(biāo)控件原本居中顯示赊瞬,設(shè)置偏移量后位置改變

偏移

4. Circular positioning圓形定位

您可以以一定角度和距離將一個小部件中心相對于另一個小部件中心進行約束先煎。
layout_constraintCircle 定位控件ID
layout_constraintCircleRadius兩個控件中心點之間的距離
layout_constraintCircleAngle角度,取值范圍0-360的整數(shù)

官方截圖

5. Visibility behavior可見性處理

與其他layout的區(qū)別在于
  • 當(dāng)控件設(shè)為GONE時巧涧,被認(rèn)為尺寸為0薯蝎,可以理解為布局上一個點
  • GONE的控件對其他控件有約束,約束保留并生效谤绳,當(dāng)margin會清零

6. Dimension constraints尺寸約束

6.1 為ContraintLayout這只最大最小寬高

當(dāng)ContraintLayout寬高設(shè)為WRAP_CONTENT時可以設(shè)置最大最小寬高

  • android:minWidth占锯、android:minHeight最小寬高
  • android:maxWidthandroid:maxHeight最大寬高
6.2 設(shè)置控件寬高

設(shè)置android:layout_width,android:layout_height
參數(shù):

  • 明確設(shè)置大小缩筛,如1dp
  • 設(shè)為WRAP_CONTENT消略,控件自己計算大小
  • 設(shè)為0dp,根據(jù)約束條件顯示大小
    注意:不建議在ConstarintLayout中使用MATCH_PARENT瞎抛,建議設(shè)為0dp艺演,約束條件中控件使用parnet
6.3 強制約束

當(dāng)控件設(shè)為WRAP_CONTENT時,添加約束尺寸是不生效的桐臊,想要生效胎撤,需要添加如下屬性

  • app:layout_constrainedWidth=”true|false”
  • app:layout_constrainedHeight=”true|false”
6.4 MATCH_CONSTRAINT
  • 改變最大最小,可以設(shè)固定dp數(shù)字断凶,也可設(shè)為wrap(相當(dāng)于WRAP_CONTENT)
    layout_constraintWidth_min伤提、layout_constraintHeight_minlayout_constraintWidth_max认烁、layout_constraintHeight_max
  • 設(shè)置百分比模式
    1. 尺寸大小設(shè)為0dp
    2. 設(shè)置百分比模式app:layout_constraintWidth_default="percent"app:layout_constraintHeight_default="percent"
    3. 設(shè)置所占大兄啄小(0-1)layout_constraintWidth_percentlayout_constraintHeight_percent
  • 控件寬高按照比例計算大小
    1. 控件寬高至少一個為0dp
    2. 設(shè)置屬性app:layout_constraintDimensionRatio
      屬性取值可以是寬度比高度的比例(float),也可以是固定格式 "width:height"砚著,也可以是前綴W或H次伶,指定一邊相對于另一邊的尺寸,如”H, 16:9”稽穆,高比寬為16:9

7. Chains 鏈條

在同一個軸上(水平或垂直)冠王,兩個或兩個以上控件首尾雙向連接,則將視為鏈條舌镶。鏈條由鏈條頭(最左側(cè)或左上方View)設(shè)置的屬性控制
  • 如果在連接上指定邊距柱彻,將被使用,如在最左側(cè)添加margin餐胀,則整個鏈條左側(cè)有了margin效果
  • 通過設(shè)置layout_constraintHorizontal_chainStyle哟楷、layout_constraintVertical_chainStyle屬性來控制鏈條樣式。取值如下
    • spread 默認(rèn)值否灾,元素平均散開展示,兩端顯示邊距
    • 權(quán)重鏈條卖擅,在spread模式下,如果控件設(shè)為MATCH_CONSTRAINT,將會占用可用空間惩阶,設(shè)置layout_constraintHorizontal_weightlayout_constraintVertical_weight控制權(quán)重(類似LinearLayoutweight
    • spread_inside 類似 spread模式挎狸,兩端不會展示margin
    • packed控件壓縮在一起,兩端均勻展示margin断楷,通過設(shè)置鏈條頭偏移量可以控制鏈條整體偏移位置
      鏈條樣式
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末锨匆,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子冬筒,更是在濱河造成了極大的恐慌恐锣,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,331評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舞痰,死亡現(xiàn)場離奇詭異土榴,居然都是意外死亡,警方通過查閱死者的電腦和手機响牛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,372評論 3 398
  • 文/潘曉璐 我一進店門鞭衩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人娃善,你說我怎么就攤上這事∪鹋澹” “怎么了聚磺?”我有些...
    開封第一講書人閱讀 167,755評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長炬丸。 經(jīng)常有香客問我瘫寝,道長,這世上最難降的妖魔是什么稠炬? 我笑而不...
    開封第一講書人閱讀 59,528評論 1 296
  • 正文 為了忘掉前任焕阿,我火速辦了婚禮,結(jié)果婚禮上首启,老公的妹妹穿的比我還像新娘暮屡。我一直安慰自己,他們只是感情好毅桃,可當(dāng)我...
    茶點故事閱讀 68,526評論 6 397
  • 文/花漫 我一把揭開白布褒纲。 她就那樣靜靜地躺著,像睡著了一般钥飞。 火紅的嫁衣襯著肌膚如雪莺掠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,166評論 1 308
  • 那天读宙,我揣著相機與錄音彻秆,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛唇兑,可吹牛的內(nèi)容都是我干的酒朵。 我是一名探鬼主播,決...
    沈念sama閱讀 40,768評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼幔亥,長吁一口氣:“原來是場噩夢啊……” “哼耻讽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起帕棉,我...
    開封第一講書人閱讀 39,664評論 0 276
  • 序言:老撾萬榮一對情侶失蹤针肥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后香伴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體慰枕,經(jīng)...
    沈念sama閱讀 46,205評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,290評論 3 340
  • 正文 我和宋清朗相戀三年即纲,在試婚紗的時候發(fā)現(xiàn)自己被綠了具帮。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,435評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡低斋,死狀恐怖蜂厅,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情膊畴,我是刑警寧澤掘猿,帶...
    沈念sama閱讀 36,126評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站唇跨,受9級特大地震影響稠通,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜买猖,卻給世界環(huán)境...
    茶點故事閱讀 41,804評論 3 333
  • 文/蒙蒙 一改橘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧玉控,春花似錦飞主、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,276評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至懒叛,卻和暖如春丸冕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背薛窥。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工胖烛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留眼姐,地道東北人。 一個月前我還...
    沈念sama閱讀 48,818評論 3 376
  • 正文 我出身青樓佩番,卻偏偏與公主長得像众旗,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子趟畏,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,442評論 2 359

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