ConstraintLayout:1.1.3的使用

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

常用布局

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

//兩個控件的左邊對齊
layout_constraintLeft_toLeftOf
//當前控件的左邊與目標控件的右邊對齊,即當前控件在目標控件的右邊
layout_constraintLeft_toRightOf
//當前控件的右邊與目標控件的左邊對齊,即當前控件在目標控件的左邊
layout_constraintRight_toLeftOf
//兩個控件的右邊對齊
layout_constraintRight_toRightOf
//兩個控件的上面對齊
layout_constraintTop_toTopOf
//當前控件的頂部與目標控件的底部對齊,即當前控件在目標控件的下面
layout_constraintTop_toBottomOf
//當前控件的底部與目標控件的頂部對齊,即當前控件在目標控件的上面
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>
圓形定位

強制約束

這個屬性是用于當約束布局中的一個控件,由于其內(nèi)容很長,導致該控件的寬度或者高度超出了范圍,導致布局上的錯亂,針對這個問題,ConstraintLayout很友善的提供了layout_constrainedWidth這個屬性,當設(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)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,702評論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,615評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,606評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,044評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,826評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,227評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,307評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,447評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,992評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,807評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,001評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,550評論 5 361
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,243評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,667評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,930評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,709評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,996評論 2 374

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