ConstraintLayout:1.1.3的使用

ConstraintLayout最新的版本是1.1.3,這是一個很優(yōu)秀的布局栽连,幾乎可以實現(xiàn)用一級布局翩伪,就能實現(xiàn)復(fù)雜的頁面。
接下來我們深入淺出的來學(xué)習(xí)一下這個牛逼轟轟的控件吧眉尸。

常用布局

這邊的布局有點類似RelativeLayout域蜗,但ConstraintLayout本身就比RelativeLayout強大。

//兩個控件的左邊對齊
layout_constraintLeft_toLeftOf
//當(dāng)前控件的左邊與目標控件的右邊對齊噪猾,即當(dāng)前控件在目標控件的右邊
layout_constraintLeft_toRightOf
//當(dāng)前控件的右邊與目標控件的左邊對齊霉祸,即當(dāng)前控件在目標控件的左邊
layout_constraintRight_toLeftOf
//兩個控件的右邊對齊
layout_constraintRight_toRightOf
//兩個控件的上面對齊
layout_constraintTop_toTopOf
//當(dāng)前控件的頂部與目標控件的底部對齊,即當(dāng)前控件在目標控件的下面
layout_constraintTop_toBottomOf
//當(dāng)前控件的底部與目標控件的頂部對齊袱蜡,即當(dāng)前控件在目標控件的上面
layout_constraintBottom_toTopOf
//兩個控件的下面對齊
layout_constraintBottom_toBottomOf
//baseline對齊丝蹭,常用于和EditText 對齊
layout_constraintBaseline_toBaselineOf
一個簡單的例子
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:padding="10dp">
    <TextView
        android:id="@+id/tv_common_layout_label"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="常用布局"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <Button
        android:id="@+id/btn_e"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_width="wrap_content"
        android:text="E"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_common_layout_label"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="A"
        app:layout_constraintBottom_toTopOf="@id/btn_e"
        app:layout_constraintRight_toLeftOf="@id/btn_e"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="B"
        app:layout_constraintBottom_toTopOf="@id/btn_e"
        app:layout_constraintLeft_toRightOf="@id/btn_e"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="C"
        app:layout_constraintRight_toLeftOf="@id/btn_e"
        app:layout_constraintTop_toBottomOf="@id/btn_e"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="D"
        app:layout_constraintLeft_toRightOf="@id/btn_e"
        app:layout_constraintTop_toBottomOf="@id/btn_e"/>
</android.support.constraint.ConstraintLayout>
常用布局

Chain(鏈)

顧名思義,就是把幾個控件鏈在一起坪蚁,以達到可以控制布局的效果奔穿,這個效果也是很常用的。
常用屬性:

layout_constraintHorizontal_chainStyle
layout_constraintVertical_chainStyle

上述兩個屬性只能在鏈的第一個控件上使用敏晤,在之后的控件使用無效贱田。
這兩個屬性的值有三個:spread_inside,spread嘴脾,packed
看一下官方提供的說明圖:


chain.png

Packed Chain with Bias是packed屬性加layout_constraintHorizontal_bias屬性
Weighted Chain的用法與之前LinearLayout中的weight比較類似男摧,就不細說了。
舉個栗子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <TextView
        android:id="@+id/tv_chain_style_label"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Chain"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <RadioGroup
        android:id="@+id/rg_chain_group"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_chain_style_label">
        <RadioButton
            android:checked="true"
            android:id="@+id/rb_spread_inside"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="spread_inside"/>
        <RadioButton
            android:id="@+id/rb_spread"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="spread"/>
        <RadioButton
            android:id="@+id/rb_pack"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="pack"/>
    </RadioGroup>
    <Button
        android:id="@+id/btn_fgh_toggle"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="FGH批量隱藏"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/rg_chain_group"/>
    <android.support.constraint.Group
        android:id="@+id/group_btns"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:constraint_referenced_ids="btn_f,btn_g,btn_h"/>
    <Button
        android:id="@+id/btn_f"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:text="F"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/btn_g"
        app:layout_constraintTop_toBottomOf="@id/btn_fgh_toggle"/>
    <Button
        android:id="@+id/btn_g"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:text="G"
        app:layout_constraintLeft_toRightOf="@id/btn_f"
        app:layout_constraintRight_toLeftOf="@+id/btn_h"
        app:layout_constraintTop_toTopOf="@id/btn_f"/>
    <Button
        android:id="@+id/btn_h"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:text="H"
        app:layout_constraintLeft_toRightOf="@id/btn_g"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/btn_g"/>
</android.support.constraint.ConstraintLayout>
class ChainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_chain_style)
        rg_chain_group.setOnCheckedChangeListener { _, checkedId ->
            constraintLayout.getViewWidget(btn_f).horizontalChainStyle = when (checkedId) {
                R.id.rb_spread_inside -> ConstraintLayout.LayoutParams.CHAIN_SPREAD_INSIDE
                R.id.rb_spread -> ConstraintLayout.LayoutParams.CHAIN_SPREAD
                R.id.rb_pack -> ConstraintLayout.LayoutParams.CHAIN_PACKED
                else -> ConstraintLayout.LayoutParams.CHAIN_PACKED
            }
            constraintLayout.requestLayout()
        }
        btn_fgh_toggle.setOnClickListener {
            if (group_btns.visibility == View.VISIBLE) {
                group_btns.visibility = View.GONE
                btn_fgh_toggle.text = "fgh批量顯示"
            } else {
                group_btns.visibility = View.VISIBLE
                btn_fgh_toggle.text = "fgh批量隱藏"
            }
        }
    }
}

這里還穿插了一個Group的用法

Group

顧名思義就是把幾個View分成一組,可以統(tǒng)一對其顯隱藏進行控制耗拓,好處就是終于不用再寫一堆的布局顯隱藏拇颅,也不會容易出錯啦。
因為比較簡單乔询,所以也不細說了蔬蕊。

Barrier,屏障猜扮,障礙

這是一個控件,可以使用constraint_referenced_ids來引用多個控件监婶,從而可以得到這組控件的最左邊/最頂部/最右邊/最底部了旅赢。
終于我們做某些要填寫信息的布局時,可以不用TableLayout惑惶,也可以不用去計算哪個標簽長度最長煮盼,估算出一個不太靠譜的長度。
常用的屬性:

//指定方向带污,不可以設(shè)置多個方向鱼冀,如果需要多個方向报破,則需要設(shè)置多個Barrier
barrierDirection
//關(guān)聯(lián)id
constraint_referenced_ids

舉個栗子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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_height="match_parent"
    android:layout_width="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity">
    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:barrierDirection="end"
        app:constraint_referenced_ids="tv_age_label,tv_name_label"/>
    <TextView
        android:id="@+id/tv_name_label"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:padding="10dp"
        android:text="姓名:"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <EditText
        android:hint="請輸入姓名"
        android:id="@+id/edt_name"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        app:layout_constrainedWidth="true"
        app:layout_constraintBaseline_toBaselineOf="@id/tv_name_label"
        app:layout_constraintLeft_toRightOf="@+id/barrier"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/tv_name_label"/>
    <TextView
        android:id="@+id/tv_age_label"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:padding="10dp"
        android:text="出生時間:"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/edt_name"/>
    <EditText
        android:hint="請輸入出生日期"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        app:layout_constraintBaseline_toBaselineOf="@id/tv_age_label"
        app:layout_constraintLeft_toRightOf="@+id/barrier"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/tv_age_label"/>
</android.support.constraint.ConstraintLayout>
Barrier.png

圓形定位

這個就有點吊了,控件A可以在控件B周圍的任何位置充易。
常用的場景一般就是設(shè)置小紅點了盹靴,未讀數(shù)之類的。
使用屬性有三個

//圓形定位的目標控件瑞妇,在這個屬性設(shè)置目標的id
layout_constraintCircle
//圓形定位的角度
layout_constraintCircleAngle
//圓形定位的半徑
layout_constraintCircleRadius

來看一張官網(wǎng)的圖:


image.png

需要注意的是稿静,0°的位置是在目標控件的頂部。

來看一個時鐘表盤的布局吧
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:layout_width="match_parent">
    <TextView
        android:id="@+id/tv_circle_label"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:text="Circle定位"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
    <TextView
        android:id="@+id/btn_0"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:layout_width="wrap_content"
        android:text="0"
        android:textColor="@android:color/black"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_circle_label"/>
    <TextView
        android:id="@+id/btn_1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="1"
        android:textColor="@android:color/black"
        app:layout_constraintCircle="@id/btn_0"
        app:layout_constraintCircleAngle="30"
        app:layout_constraintCircleRadius="80dp"/>
    <TextView
        android:id="@+id/btn_2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="2"
        android:textColor="@android:color/black"
        app:layout_constraintCircle="@id/btn_0"
        app:layout_constraintCircleAngle="60"
        app:layout_constraintCircleRadius="80dp"/>
    <TextView
        android:id="@+id/btn_3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="3"
        android:textColor="@android:color/black"
        app:layout_constraintCircle="@id/btn_0"
        app:layout_constraintCircleAngle="90"
        app:layout_constraintCircleRadius="80dp"/>
        ...
</android.support.constraint.ConstraintLayout>
圓形定位

強制約束

這個屬性是用于當(dāng)約束布局中的一個控件踪宠,由于其內(nèi)容很長自赔,導(dǎo)致該控件的寬度或者高度超出了范圍,導(dǎo)致布局上的錯亂柳琢,針對這個問題绍妨,ConstraintLayout很友善的提供了layout_constrainedWidth這個屬性润脸,當(dāng)設(shè)置為true時,會強制控件的寬高會在一個范圍內(nèi)他去,false則不強制毙驯。
我們來看個栗子:


layout_constrainedWidth為false

layout_constrainedWidth為true

代碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:layout_width="match_parent">
    <CheckBox
        android:checked="true"
        android:id="@+id/cb_force_constraint"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:text="enforcing constraints"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
    <Button
        android:id="@+id/btn_add_text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="add"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/btn_reduce_text"
        app:layout_constraintTop_toBottomOf="@id/cb_force_constraint"/>
    <Button
        android:id="@+id/btn_reduce_text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="reduce"
        app:layout_constraintBaseline_toBaselineOf="@id/btn_add_text"
        app:layout_constraintLeft_toRightOf="@id/btn_add_text"
        app:layout_constraintRight_toRightOf="parent"/>
    <Button
        android:id="@+id/btn_m"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="M"
        app:layout_constraintTop_toBottomOf="@id/btn_add_text"/>
    <Button
        android:id="@+id/btn_content"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_constrainedWidth="true"
        android:text="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        app:layout_constraintLeft_toRightOf="@id/btn_m"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn_m"/>
</android.support.constraint.ConstraintLayout>

Percent和Ratio

百分比布局也是UI設(shè)計師非常喜歡使用的,android支持包中原先有提供了一系列的百分比控件


image.png

寬高比布局android中就沒有了灾测,一般都是我們先固定寬或者高爆价,再計算出另一個邊
但是有了ContraintLayout,就可以通通不要了媳搪,原因很簡單铭段,俺們的ContraintLayout超棒的~
我們可以直接通過以下這個屬性來直接進行百分比控制:

layout_constraintDimensionRatio

這個屬性要怎么玩呢,其實也不復(fù)雜:
假如是固定寬度秦爆,要控制高度序愚,可以用"H,16:9"。
假如是固定高度等限,要控制寬度爸吮,可以用"W,16:9"
冒號左邊的是代表寬度,冒號右邊的是代表高度望门。
依舊來舉個栗子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_height="match_parent" android:layout_width="match_parent">
    <TextView
        android:id="@+id/tv_title"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="百分比布局"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <Button
        android:id="@+id/btn_ratio_1"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:text="Ratio_1"
        app:layout_constraintDimensionRatio="H,16:9"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_title"
        app:layout_constraintWidth_percent="0.6"/>
    <Button
        android:id="@+id/btn_ratio_2"
        android:layout_height="100dp"
        android:layout_width="0dp"
        android:text="Ratio_2"
        app:layout_constraintDimensionRatio="W,16:9"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn_ratio_1"/>
</android.support.constraint.ConstraintLayout>
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末形娇,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子筹误,更是在濱河造成了極大的恐慌桐早,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纫事,死亡現(xiàn)場離奇詭異勘畔,居然都是意外死亡所灸,警方通過查閱死者的電腦和手機丽惶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來爬立,“玉大人钾唬,你說我怎么就攤上這事∠姥保” “怎么了抡秆?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長吟策。 經(jīng)常有香客問我儒士,道長,這世上最難降的妖魔是什么檩坚? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任着撩,我火速辦了婚禮诅福,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拖叙。我一直安慰自己氓润,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布薯鳍。 她就那樣靜靜地躺著咖气,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挖滤。 梳的紋絲不亂的頭發(fā)上崩溪,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機與錄音斩松,去河邊找鬼悯舟。 笑死,一個胖子當(dāng)著我的面吹牛砸民,可吹牛的內(nèi)容都是我干的抵怎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼岭参,長吁一口氣:“原來是場噩夢啊……” “哼反惕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起演侯,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤姿染,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后秒际,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體悬赏,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年娄徊,在試婚紗的時候發(fā)現(xiàn)自己被綠了闽颇。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡寄锐,死狀恐怖兵多,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情橄仆,我是刑警寧澤剩膘,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站盆顾,受9級特大地震影響怠褐,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜您宪,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一奈懒、第九天 我趴在偏房一處隱蔽的房頂上張望具温。 院中可真熱鬧,春花似錦筐赔、人聲如沸铣猩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽达皿。三九已至,卻和暖如春贿肩,著一層夾襖步出監(jiān)牢的瞬間峦椰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工汰规, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留汤功,地道東北人。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓溜哮,卻偏偏與公主長得像滔金,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子茂嗓,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,652評論 2 354

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