Android ConstraintLayout使用指南

2021-02-20 技術視界

升級Android Studio 2.3之後,IDE默認生成的Activity布局都是以ConstraintLayout做為根布局,體驗了一把這個Google去年就開始力推的ConstraintLayout後,覺得非常不錯,本文用於記錄ConstraintLayout各個方面的使用知識。

ConstraintLayout最低兼容Android 2.3;

目前Android Studio 2.3默認使用ConstraintLayout作為布局文件的根布局;

想要使用ConstraintLayout,需在項目的build.gradle添加com.android.support.constraint:constraint-layout:XXX版本號依賴;

ConstraintLayout翻譯成中文也稱為約束布局,在整個使用體驗過程中真的是貫穿約束二字,這一節先來介紹一些基礎使用,後面你就會慢慢感受到約束布局的魅力。創建完工程後打開布局文件,底部切換Design的Tab上,可以看到整個操作界面

左上角的面板是放置了系統內置各種各樣的控制項,想要布局直接拖到到布局文件中即可(所見即所得),右邊的面板是選中布局文件中的控制項時期各種各樣的空間屬性,ConstraintLayout最大的好處在於讓我們通過拖控制項的形式進行布局,並且不用擔心適配問題。所以,先來拖個控制項試試看,將一個Button拖動到屏幕正中央,然後運行顯示看看效果。

而實際運行後卻發現,這個Button還是位於屏幕左上角,說好的居中效果呢?這裡就要開始引入ConstraintLayout的約束概念,我們切換回去看xml的布局代碼,發現了兩個問題。第一,布局預覽時能夠看到顯示居中的Button,是因為控制項屬性設置中引用了兩個tools命名空間下的屬性。

我們都知道,這兩個屬性只在布局編輯器的預覽中有效,實際運行效果是不生效的。第二,Button標籤下有紅色波浪線警告,我們把滑鼠移到對應位置會發現警告內容,告訴我們Button沒有任何約束設置,當前效果只支持預覽,實際運行後會返回到左上角去,同時提示我們應該給控制項添加約束。

如何增加約束?回到Design頁面,對著控制項上下左右四個原點拖動添加對應的約束即可。

成功添加約束後,即可得到正常的運行效果了。

實際操作不一定要在Tab,也可以直接在Text頁面拖動控制項添加約束。

成功實現添加約束後,可以看到Button多了下面幾個屬性設置

app:layout_constraintBottom_toBottomOf="parent" 意思是Button底部的布局約束是位於parent的底部,parent是指包裹著它的ConstraintLayout,也可以設置指定某個控制項的id,其他類似的屬性就不再贅述,以上四個約束聯合起來便實現了Button的居中,ConstraintLayout總共有下面這些同類的屬性

你會發現ConstraintLayout非常靈活的把RelativeLayout的活給幹了,關於left、right、top、bottom、start、end、baseline的基準可以參照下圖

如果我想加多一個Button2並且將其放置到原先居中Button的右方並且與其底部對齊,只需如下操作即可

並且你也可以發現,Button2依賴與Button後會隨著Button的移動而跟著發生相對移動,目的是了保證我設置的依賴,時刻保持Button2就在Button的右邊,並且底部對齊。你也可以看到布局文件中也為Button2添加了如下兩個屬性

如果你已經理解上面提到的屬性含義,這裡應該不會有疑惑。

介紹完上下左右的依賴設置後,下面介紹一些Margin屬性,除了Android常見的各種android:layout_marginXXX外,ConstraintLayout自行添加了如下屬性

這些設置生效於當依賴的約束對象被設置visibility為gone時,非常簡單,讀者自行設置實踐對比即可,這裡就不展示效果了。

聊完Margin後,再來介紹一下Bias,ConstraintLayout新增了如下兩個屬性用於控制控制項在水平和垂直方向在屏幕上的偏移比例

當為目標控制項設置好橫縱向的約束時(app:layout_constraintLeft_toLeftOf="parent"、app:layout_constraintRight_toRightOf="parent"或者app:layout_constraintTop_toTopOf="parent"、app:layout_constraintBottom_toBottomOf="parent"),這個兩個屬性才會生效。實際操作過程中,你會發現對著設置好橫縱向約束的Button進行拖動,布局中的layout_constraintHorizontal_bias和layout_constraintVertical_bias會一直發生相應的變化,如果你需要Button居中,那麼直接將這兩個屬性的參數值設置為0.5即可。

關於ConstraintLayout的基本使用,這一節就介紹到這裡。

看完基本使用,再來學習一些進階技巧,這裡先補充一個關於ConstraintLayout的知識點,與其他Layout不同之處在於,它的layout_width和layout_height不支持設置match_parent,其屬性取值只有以下三種情況:

想想如果沒有ConstraintLayout,我們要讓一個控制項的寬高按某個比例進行布局應該怎麼做?有了ConstraintLayout後,我們可以使用layout_constraintDimentionRatio屬性設置寬高比例,前提是目標控制項的layout_width和layout_height至少有一個設置為0dp,如下讓一個ImageView寬高按照2:1的比例顯示

layout_constraintDimentionRatio默認參數比例是指寬:高,變成高:寬可以設app:layout_constraintDimensionRatio="H,2:1"。

ConstraintLayout的鏈條(Chains)特性非常強大,在沒有ConstraintLayout之前,線性布局我們主要都依靠LinearLayout來完成,有了ConstraintLayout之後,它把LinearLayout的活也幹了,例如要把按鈕水平排成一行,可以這樣操作

這樣ButtonA、B、C就在水平方向形成了一條Chain,並且底部對齊。回去看xml文件,會見到ButtonA新增app:layout_constraintHorizontal_chainStyle的屬性設置,這個屬性在一條Chain中只會出現在第一個控制項中,這個控制項是整條Chain的Head。

除了水平方向的layout_constraintHorizontal_chainStyle外還有垂直方向的layout_constraintVertical_chainStyle,兩者均有spread,spread_inside,packed這三種取值,如果將控制項的layout_width和layout_height設置成為0dp,還可以配合layout_constraintHorizontal_weight、layout_constraintVertical_weight兩個屬性實現和LinearLayout中設置layout_weight相同的效果,具體的操作這裡就不再展示了,下面一張圖告訴你Chain的靈活強大之處。

關於Chain的就介紹到此,進階的最後一部分再介紹一下Guideline功能,如果我們需要對著屏幕的中軸線進行布局,就可以使用到Guideline進行操作,例如下面兩個Button分別分布在中軸線的左右兩側

從操作上我們可以看到Guideline也分為垂直和水平兩種,並且支持設置在屏幕中所處的位置,可以使用layout_constraintGuide_begin和layout_constraintGuide_end設置具體dp值,也可以使用layout_constraintGuide_percent來設置比例。實際上它也只是一個輔助我們布局的View而已,其源碼內部實現也非常簡單,並且默認設置了visibility為gone,關於ConstraintLayout的進階使用便介紹到這裡。

關於ConstraintLayout的使用優勢,我個人體驗總結如下:

高效布局,Android這麼多年以來真正意義上的實現了所見即所得的拖曳方式布局,極大的提高開發效率;

輕鬆靈活的實現複雜的布局;

解決多重布局嵌套問題,通過前面介紹你會發現ConstraintLayout真的是非常靈活,可以很大程度的避免Layout之間的嵌套;

滿足屏幕適配的需求,想想沒有ConstraintLayout之前的拖曳式布局,你就知道有多噁心;

ConstraintLayout最開始出來就有很多開發者盼著完全依賴於拖曳方式實現布局,而在實際操作過程中,完全通過拖曳其實效率反倒是會打折扣,在此建議是拖曳方式和xml編碼相結合才是最佳實踐的方式。覺得文章不錯可以幫忙支持轉發哦!

歡迎關注公眾號「技術視界」

相關焦點

  • Android ConstraintLayout約束布局可視化工具使用~
    ,一般新建項目會默認添加這個依賴如果沒有也可以手動添加,截止完稿最新版本就是 1.0.2 版本,添加完依賴就能夠使用了compile 'com.android.support.constraint:constraint-layout:1.0.2'
  • 帶你了解 Android 約束布局 ConstraintLayout
    所以極力推薦使用ConstraintLayout來編寫布局。本文主要介紹一下如何使用代碼來編寫ConstraintLayout布局。好了,開始我們的徵程。ConstraintLayout簡介ConstraintLayout,可以翻譯為約束布局,在2016年Google I/O 大會上發布。我們知道,當布局嵌套過多時會出現一些性能問題。
  • Android MotionLayout動畫:續寫ConstraintLayout新篇章
    android:layout_marginTop="50dp"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent
  • ConstraintLayout 介紹
    通常的布局邊距屬性可以這樣使用:android:layout_marginStartandroid:layout_marginEndandroid:layout_marginLeftandroid:layout_marginTop
  • 強大的 ConstraintLayout
    ,所以這篇文章基本上算是重寫了一遍,有任何不足請大家提出使用方式首先這個布局是要添加 依賴的,一般新建項目會默認添加這個依賴如果沒有也可以手動添加,截止完稿最新版本就是 1.0.2 版本,添加完依賴就能夠使用了compile 'com.android.support.constraint:constraint-layout:1.0.2
  • MotionLayout系列之配合布局CoordinatorLayout, DrawerLayout, ViewPager使用
    在 Coordinatorlayout 中使用 MotionLayout:( MotionLayout 可以實現類似 CoodinatorLayout 的功能,我們將在以後的文章中提供示例)可以通過 MotionLayout 指定一部分 View 的動畫,將更多有趣的動畫加到已經存在的布局中。
  • 是時候讓 Android Tools 屬性拯救你了
    相似的例子還可以在使用 ImageView 的時候看到:<ImageView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:src="@drawable/ic_person_off"  tools
  • Android 這些 Drawable 你都會用嗎?
    app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf
  • Android子線程也能修改UI?
    ><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http
  • Android 自定義View 點讚效果
    ><android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:agreeview="http://schemas.android.com/apk/res-auto"    xmlns:tools
  • Google 開源的 Android 排版庫:FlexboxLayout
    這使用起來一個布局就可以搞定各種複雜的情況了,於是 FlexboxLayout 就應運而生了。xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:flexWrap="wrap"    app:alignItems="stretch"    app:alignContent
  • Android 8.0 LinearLayout 源碼解析
    在不使用 android:layout_weight 屬性時,LinearLayout 的 onMeasure 流程還是比較簡單的,只會進入第一個 for 循環遍歷所有的 childView 並計算他們的大小,如果使用了 android:layout_weight 屬性則會進入第三個 for 循環並再次遍歷所有的 childView,再次重新執行 childView 的 measure() 方法。
  • 寫給Android開發的Gradle知識體系
    }    }}dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    implementation 'com.android.support:appcompat-v7:28.0.0'    implementation 'com.android.support.constraint
  • 通關Android Lint
    ><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" -添加命名空間 xmlns:tools="http
  • 如何在Xamarin.Android中使用約束布局
    layout_constraintLeft_toLeftOf-將所需視圖的左側對齊到目標視圖的左側。layout_constraintLeft_toRightOf-將所需視圖的左側對齊到目標視圖的右側。
  • 我用ConstraintLayout寫了個登錄頁面,原來是那麼的好用
    app:layout_constraintBottom_toBottomOf="@+id/view1"與之不同的是RelativeLayout,ConstraintLayout提供bias用於相對於手柄以0%和100%水平和垂直偏移定位視圖的值(用圓圈標記)。這些百分比(和分數)提供了跨不同屏幕密度和大小的視圖的無縫定位。
  • 使用Junit對Android應用進行單元測試
    ><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="fill_parent"    android:layout_height="fill_parent">
  • 輕鬆掌握RelativeLayout相對布局
    android:layout_centerHorizontal:控制該組件是否和布局容器的水平居中。android:layout_centerVertical:控制該組件是否和布局容器的垂直居中。android:layout_centerInparent:控制該組件是否和布局容器的中央位置。
  • 【精講】CoordinatorLayout與滾動的處理
    浮動操作按鈕與SnackbarCoordinatorLayout可以用來配合浮動操作按鈕的 layout_anchor 和 layout_gravity屬性創造出浮動效果,layout_anchor 指定參照物, anchorGravity 指定相對於參照物的位置,設置為 bottom
  • android絕對布局
    -- 定義一個文本框,使用絕對定位 -->09<TextView10android:layout_x="20dip"11android:layout_y="20dip"12android:layout_width="wrap_content"13android:layout_height="wrap_content"