1.android布局-ConstraintLayout-約束布局

ConstraintLayout 是什么?

ConstraintLayout

ConstraintLayout 怎么用己沛?

基本用法

layout_constraint[當前控件位置]_[目標控件位置]="[目標控件ID]"

1.上下排列

a  
b  

b:
app:layout_constraintTop_toBottomOf="a"

實例:

<android.support.constraint.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">
    <TextView
        android:id="@+id/a"
        style="@style/text"
        android:text="@string/a" />

    <TextView
        android:id="@+id/b"
        style="@style/text"
        android:text="@string/b"
        app:layout_constraintTop_toBottomOf="@id/a" />
</android.support.constraint.ConstraintLayout>

2.左右排列

a b
a:
app:layout_constraintRight_toLeftOf="b"
b:
app:layout_constraintLeft_toRightOf="a"

實例:

<android.support.constraint.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">
    <TextView
        android:id="@+id/a"
        style="@style/text"
        app:layout_constraintRight_toLeftOf="@id/b"
        android:text="@string/a" />

    <TextView
        android:id="@+id/b"
        style="@style/text"
        android:text="@string/b"
        app:layout_constraintLeft_toRightOf="@id/a" />
</android.support.constraint.ConstraintLayout>

左右排列更嚴謹?shù)膶懛?/p>

a b
b:
app:layout_constraintStart_toEndOf="a"
app:layout_constraintTop_toTopOf="a"
app:layout_constraintBottom_toBottomOf="a"

實例:

<android.support.constraint.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">
    <TextView
        android:id="@+id/a"
        style="@style/text"
        app:layout_constraintRight_toLeftOf="@id/b"
        app:layout_constraintEnd_toStartOf="@id/b"
        app:layout_constraintTop_toTopOf="@id/b"
        android:text="@string/a" />

    <TextView
        android:id="@+id/b"
        style="@style/text"
        android:text="@string/b"
        app:layout_constraintStart_toEndOf="@id/a"
        app:layout_constraintTop_toTopOf="@id/a"
        app:layout_constraintLeft_toRightOf="@id/a" />
</android.support.constraint.ConstraintLayout>

3.靠最右邊

a 靠界面最右邊
a:
app:layout_constraiontEnd_toEndOf="parent"

實例:

<android.support.constraint.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">
    <TextView
        android:id="@+id/a"
        style="@style/text"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="@string/a" />
</android.support.constraint.ConstraintLayout>
constraiontEnd 是自己本身的
toEnd 底部停靠的
parent 是父布局

4.三等分然后突茫靠最底部

a b c
a:
app:layout_constraiontBottom_toBottomOf="parent"
app:layout_constraiontLeft_toLeftOf="parent"
app:layout_constraiontRight_toLeftOf="b"

b:
app:layout_constraiontBottom_toBottomOf="parent"
app:layout_constraiontLeft_toRightOf="a"
app:layout_constraiontRight_toLeftOf="c"

c:
app:layout_constraiontBottom_toBottomOf="parent"
app:layout_constraiontLeft_toRightOf="b"
app:layout_constraiontRight_toRightOf="parent"

上面就是構成官網(wǎng)所說的鏈(chain)

實例:

<android.support.constraint.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">
    <TextView
        android:id="@+id/a"
        style="@style/text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/b"
        android:text="@string/a" />

    <TextView
        android:id="@+id/b"
        style="@style/text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/a"
        app:layout_constraintRight_toLeftOf="@id/c"
        android:text="@string/b"/>
    <TextView
        android:id="@+id/c"
        style="@style/text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/b"
        android:text="@string/c"/>
</android.support.constraint.ConstraintLayout>

延伸兩個屬性

app:layout_constraiontHorizontal_weight="1" //等分屬性

app:layout_constraiontHorizontal_chainstyle="spread_inside" //spread_inside 兩端對齊 packed 聚中  spread(默認)控件之間留空

實例:
聚中:

<android.support.constraint.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">
    <TextView
        android:id="@+id/a"
        style="@style/text"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/b"
        android:text="@string/a" />

    <TextView
        android:id="@+id/b"
        style="@style/text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/a"
        app:layout_constraintRight_toLeftOf="@id/c"
        android:text="@string/b"/>
    <TextView
        android:id="@+id/c"
        style="@style/text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/b"
        android:text="@string/c"/>
</android.support.constraint.ConstraintLayout>

左右分散:

<android.support.constraint.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">
    <TextView
        android:id="@+id/a"
        style="@style/text"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/b"
        android:text="@string/a" />

    <TextView
        android:id="@+id/b"
        style="@style/text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/a"
        app:layout_constraintRight_toLeftOf="@id/c"
        android:text="@string/b"/>
    <TextView
        android:id="@+id/c"
        style="@style/text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/b"
        android:text="@string/c"/>
</android.support.constraint.ConstraintLayout>

Guideline (約束線)

實例:

<android.support.constraint.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">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintWidth_default="spread" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="@+id/button" />
</android.support.constraint.ConstraintLayout>



Guideline 是一個約束線(不會畫出來)
app:layout_constraintGuide_percent="0.5" 0.5是50%
上面代碼是 在視圖50%位置 左右兩邊有兩個butthon

實例:

<android.support.constraint.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">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="40dp"/>
    
    <TextView
        android:id="@+id/a"
        style="@style/text"
        app:layout_constraintTop_toTopOf="@id/guideline"
        android:text="@string/a"/>
    <TextView
        android:id="@+id/b"
        style="@style/text"
        app:layout_constraintTop_toTopOf="@id/guideline"
        app:layout_constraintLeft_toRightOf="@id/a"
        android:text="@string/b"/>
    <TextView
        android:id="@+id/c"
        style="@style/text"
        app:layout_constraintTop_toTopOf="@id/guideline"
        app:layout_constraintLeft_toRightOf="@id/b"
        android:text="@string/c"/>
</android.support.constraint.ConstraintLayout>

在40dp上畫一條約束線 
textview以此排列

源碼

源碼

相關鏈接

ConstraintLayout詳解
Android新特性介紹峦甩,ConstraintLayout完全解析
Constraint Layout 1.1.x帶來了哪些新東西嘱朽?
未來布局之星——ConstraintLayout
ConstraintLayout使用的一些坑
Android約束布局ConstraintLayout 項目實戰(zhàn)攻略
Android ConstraintLayout詳解
實戰(zhàn)篇ConstraintLayout的崛起之路
關于ConstraintLayout與Recycleview使用中的一些坑
ConstraintLayout使用的一些坑

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末软吐,一起剝皮案震驚了整個濱河市瘩将,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌凹耙,老刑警劉巖姿现,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異肖抱,居然都是意外死亡备典,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門意述,熙熙樓的掌柜王于貴愁眉苦臉地迎上來提佣,“玉大人,你說我怎么就攤上這事欲险「湟溃” “怎么了匹涮?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵天试,是天一觀的道長。 經(jīng)常有香客問我然低,道長喜每,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任雳攘,我火速辦了婚禮带兜,結果婚禮上,老公的妹妹穿的比我還像新娘吨灭。我一直安慰自己刚照,他們只是感情好,可當我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布喧兄。 她就那樣靜靜地躺著无畔,像睡著了一般啊楚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上浑彰,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天恭理,我揣著相機與錄音,去河邊找鬼郭变。 笑死颜价,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的诉濒。 我是一名探鬼主播周伦,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼未荒!你這毒婦竟也來了横辆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤茄猫,失蹤者是張志新(化名)和其女友劉穎狈蚤,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體划纽,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡脆侮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了勇劣。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片靖避。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖比默,靈堂內(nèi)的尸體忽然破棺而出幻捏,到底是詐尸還是另有隱情,我是刑警寧澤命咐,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布篡九,位于F島的核電站,受9級特大地震影響醋奠,放射性物質(zhì)發(fā)生泄漏榛臼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一窜司、第九天 我趴在偏房一處隱蔽的房頂上張望沛善。 院中可真熱鬧,春花似錦塞祈、人聲如沸金刁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽尤蛮。三九已至漠秋,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間抵屿,已是汗流浹背庆锦。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留轧葛,地道東北人搂抒。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像尿扯,于是被迫代替她去往敵國和親求晶。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,573評論 2 359