我們能用ConstraintLayout 做些什么?

ConstraintLayout薪寓,約束布局

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way.
這是Google官方API的介紹

ConstraintLayout是Android Studio 2.2中主要的新增功能之一亡资,也是Google在2016年的I/O大會(huì)上重點(diǎn)宣傳的一個(gè)功能。在Android Studio 2.3開(kāi)始向叉,IDE默認(rèn)生成的Activity布局都是以ConstraintLayout做為根布局锥腻。

ConstraintLayout提供了可視化的方式來(lái)編寫(xiě)界面,但是在實(shí)際使用中母谎,因?yàn)槲覀兊牟季謴?fù)雜瘦黑,很難用可視化操作去精準(zhǔn)布局,所以我們還是傾向于手動(dòng)去寫(xiě)xml。

ConstraintLayout的優(yōu)點(diǎn)

1.對(duì)比RelativeLayout

實(shí)際開(kāi)發(fā)中幸斥,我們最常用的layout是RelativeLayout存崖,ConstraintLayout某種意義上可以認(rèn)為是RelativeLayout的升級(jí)版。 舉一個(gè)簡(jiǎn)單的例子

constraint01.png

上圖中一個(gè)button在RelativeLayout的右下方睡毒,當(dāng)我們希望這個(gè)RelativeLayout的高度自適應(yīng)的時(shí)候,我們?nèi)ピO(shè)置RelativeLayout的layout heiht為wrap content冗栗,我們會(huì)看到如下圖結(jié)果演顾,顯然不是我們需要的。

constraint02.png

但是如果用ConstraintLayout隅居,可以輕松實(shí)現(xiàn)這個(gè)需求

constraint03.png

2.扁平化

我們都知道钠至,我們?cè)趯?shí)現(xiàn)界面布局的時(shí)候,強(qiáng)調(diào)盡量少的嵌套胎源,因?yàn)榍短锥鄷?huì)導(dǎo)致需要更多的時(shí)間去繪制view棉钧。為了減少嵌套,我們經(jīng)常會(huì)使用RelativeLayout代替LinearLayout涕蚤,但是RelativeLayout會(huì)對(duì)子View做兩次measure宪卿。

相關(guān)view的繪制流程以及RelativeLayout和LinearLayout的性能對(duì)比,貼一個(gè)文章Android中RelativeLayout和LinearLayout性能分析

我們看一個(gè)例子可以清晰看到ConstraintLayout減少嵌套


constraint04.png

如上圖的UI如果我們用原先的RelativeLayout實(shí)現(xiàn)万栅,層次圖如下


constraint06.png

但是如果我們用ConstraintLayout實(shí)現(xiàn)佑钾,層次圖會(huì)變的扁平化如下圖
constraint07.png

另外關(guān)于ConstraintLayout的性能分析,可以參考Google的文章解析ConstraintLayout的性能優(yōu)勢(shì)

3.應(yīng)對(duì)需求多變性

還是剛才那個(gè)例子烦粒,如果ui需求修改成如下圖

constraint05.png

如果用RelativeLayout我們可能需要很大程度上去修改原來(lái)的布局結(jié)構(gòu)休溶,但是用ConstraintLayout,我們只需要把不需要的那個(gè)Textview置為GONE就能達(dá)到UI效果扰她。
當(dāng)然兽掰,不是所有需求變動(dòng)我們都能很方便去實(shí)現(xiàn)。
貼一下這個(gè)UI的ConstraintLayout的實(shí)現(xiàn)代碼

<?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:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="64.5dp"
    android:background="@drawable/item_background_selector_white">

    <tv.chushou.zues.widget.fresco.FrescoThumbnailView
        android:id="@+id/iv_image"
        android:layout_width="@dimen/subc_user_thumb_width"
        android:layout_height="@dimen/subc_user_thumb_width"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        fresco:actualImageScaleType="centerCrop"
        fresco:placeholderImageScaleType="fitXY"
        fresco:roundAsCircle="true"
        fresco:roundingBorderColor="@color/transparent_5_black"
        fresco:roundingBorderWidth="0.5dp" />

    <ImageView
        android:id="@+id/iv_sex"
        android:layout_width="14dp"
        android:layout_height="14dp"
        android:src="@drawable/user_female_big"
        app:layout_constraintBottom_toBottomOf="@+id/iv_image"
        app:layout_constraintRight_toRightOf="@+id/iv_image" />

    <com.kascend.chushou.widget.ImgRightTextView
        android:id="@+id/img_text_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2.5dp"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        app:layout_constraintBottom_toTopOf="@+id/tv_bottom"
        app:layout_constraintEnd_toStartOf="@+id/tv_game"
        app:layout_constraintLeft_toRightOf="@+id/iv_image"
        app:layout_constraintRight_toLeftOf="@+id/tv_game"
        app:layout_constraintStart_toEndOf="@+id/iv_image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_goneMarginRight="105dp"
        app:magin_left="5dp" />

    <TextView
        android:id="@+id/tv_game"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2.5dp"
        android:layout_marginEnd="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:ellipsize="end"
        android:gravity="right"
        android:includeFontPadding="false"
        android:singleLine="true"
        android:textColor="@color/download_content_color"
        android:textSize="12.5sp"
        app:layout_constraintBottom_toTopOf="@+id/tv_state"
        app:layout_constraintEnd_toStartOf="@+id/iv_detail"
        app:layout_constraintLeft_toRightOf="@+id/img_text_name"
        app:layout_constraintRight_toLeftOf="@+id/iv_detail"
        app:layout_constraintStart_toEndOf="@+id/img_text_name"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/tv_bottom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:layout_marginTop="2.5dp"
        android:ellipsize="end"
        android:includeFontPadding="false"
        android:singleLine="true"
        android:textColor="@color/kas_littlegray"
        android:textSize="@dimen/fontsize_12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tv_state"
        app:layout_constraintLeft_toRightOf="@+id/iv_image"
        app:layout_constraintRight_toLeftOf="@+id/tv_state"
        app:layout_constraintStart_toEndOf="@+id/iv_image"
        app:layout_constraintTop_toBottomOf="@+id/img_text_name" />

    <TextView
        android:id="@+id/tv_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="2.5dp"
        android:ellipsize="end"
        android:gravity="top|right"
        android:includeFontPadding="false"
        android:singleLine="true"
        android:textColor="@color/friend_state"
        android:textSize="@dimen/fontsize_9sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/iv_detail"
        app:layout_constraintLeft_toRightOf="@+id/tv_bottom"
        app:layout_constraintRight_toLeftOf="@+id/iv_detail"
        app:layout_constraintStart_toEndOf="@+id/tv_bottom"
        app:layout_constraintTop_toBottomOf="@+id/tv_game" />

    <ImageView
        android:id="@+id/iv_detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="14dp"
        android:layout_marginRight="14dp"
        android:src="@drawable/bg_in_playlive"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:layout_width="0dp"
        android:layout_height="1px"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:background="@color/c_page_diliver_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/iv_image"
        app:layout_constraintStart_toEndOf="@+id/iv_image" />

</android.support.constraint.ConstraintLayout>

使用ConstraintLayout

幾個(gè)要點(diǎn)

  • Chains
  • Guidelines
  • ConstraintSet

更新

Constraint Layout 1.1.x帶來(lái)了哪些新東西廉涕?

關(guān)于坑

ConstraintLayout嵌套會(huì)出現(xiàn)顯示不全的問(wèn)題

其他暫時(shí)還未碰到

關(guān)于引用

文中貼了鏈接的文章都有參考泻云,致謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末狐蜕,一起剝皮案震驚了整個(gè)濱河市宠纯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌层释,老刑警劉巖婆瓜,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡廉白,警方通過(guò)查閱死者的電腦和手機(jī)个初,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)猴蹂,“玉大人院溺,你說(shuō)我怎么就攤上這事“跚幔” “怎么了珍逸?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)聋溜。 經(jīng)常有香客問(wèn)我谆膳,道長(zhǎng),這世上最難降的妖魔是什么撮躁? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任漱病,我火速辦了婚禮,結(jié)果婚禮上把曼,老公的妹妹穿的比我還像新娘杨帽。我一直安慰自己,他們只是感情好嗤军,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布睦尽。 她就那樣靜靜地躺著,像睡著了一般型雳。 火紅的嫁衣襯著肌膚如雪当凡。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,441評(píng)論 1 310
  • 那天纠俭,我揣著相機(jī)與錄音沿量,去河邊找鬼。 笑死冤荆,一個(gè)胖子當(dāng)著我的面吹牛朴则,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播钓简,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼乌妒,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了外邓?” 一聲冷哼從身側(cè)響起撤蚊,我...
    開(kāi)封第一講書(shū)人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎侦啸,沒(méi)想到半個(gè)月后槽唾,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡庞萍,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了忘闻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钝计。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖齐佳,靈堂內(nèi)的尸體忽然破棺而出葵蒂,到底是詐尸還是另有隱情,我是刑警寧澤重虑,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站秦士,受9級(jí)特大地震影響缺厉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜隧土,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一提针、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧曹傀,春花似錦辐脖、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至幕庐,卻和暖如春久锥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背异剥。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工瑟由, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人冤寿。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓歹苦,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親督怜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子殴瘦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

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