Android ConstraintLayout約束布局使用實例介紹
基本結(jié)構(gòu)
約束結(jié)構(gòu)原理:將一個組件與約束布局關(guān)聯(lián)后,通過 modifier 來指定組件位置
導(dǎo)入 compose 約束布局包
打開 build.gradle(:app)
文件
在依賴中添加以下這一行,之后點擊頂部的 sync now
進(jìn)行 gradle 配置
androidx.constraintlayout:constraintlayout-compose:1.0.1
約束布局使用步驟
- ConstraintLayout 定義一個約束布局
- val (button, text) = createRefs() 所有需要和約束布局相關(guān)聯(lián)的組件都必須要在這里進(jìn)行注冊!如果僅注冊一個組件,那就用 createRef,多個組件就用上面那樣子(這一段代碼使用了 kotlin 的解構(gòu)賦值)
- Modifier.constrainAs(button) 在組件的 modifier 中將自身與約束布局相關(guān)聯(lián)!
- top.linkTo 最后用這辦法在約束布局里面定位組件
@Composable fun ConstraintLayoutContent() { ConstraintLayout() { val (button, text) = createRefs() Button( onClick = { /*TODO*/ }, modifier = Modifier.constrainAs(button) { top.linkTo(parent.top, margin = 16.dp) } ) { Text(text = "btn") } } }
繼續(xù)約束
緊接著上面的代碼,我們指定一個 text 跟在 button 的下面
@Composable fun ConstraintLayoutContent() { ConstraintLayout() { ... Text(text = "nullclear", modifier = Modifier.constrainAs(text) { // 直接拿button作為參照點進(jìn)行布局 top.linkTo(button.bottom, margin = 16.dp) // 水平約束對齊父類 centerHorizontallyTo(parent) }) } }
參考線
渲染結(jié)果
createGuidelineFromStart
從最左側(cè)開始一定距離后構(gòu)造一個垂直參考線
fraction = 0.5f
距離(左側(cè)的)長度,0.5f 恰好是屏幕的一半長
linkTo
在這里面設(shè)置約束的左側(cè)(start)和右側(cè)(end)
Dimension.preferredWrapContent
讓文本自動換行,不破壞結(jié)構(gòu)
如果我們不加上 width 這一行的話,文本會沖破 guideline 的左側(cè)限制而超出!
@Composable fun ConstraintLayoutContent2() { ConstraintLayout { val text = createRef() val guideline = createGuidelineFromStart(fraction = 0.5f) Text(text = "helloworld ansd htiwhiqeusadjasdl d joi hdaslh dqlwh as qwe", modifier = Modifier.constrainAs(text) { linkTo(start = guideline, end = parent.end) width = Dimension.preferredWrapContent }) } }
約束解耦
直接拿上面寫好的代碼解耦了
約束集合
ConstraintSet 設(shè)置一個約束集合,在里面提前設(shè)置好各個組件的約束條件!
下方代碼分別設(shè)置了兩個組件 button 和 text 的約束條件;
private fun decoupleConstraints(margin: Dp): ConstraintSet { return ConstraintSet { val button = createRefFor("button") val text = createRefFor("text") constrain(button) { top.linkTo(parent.top, margin) } constrain(text) { top.linkTo(button.bottom, margin) } } }
解耦調(diào)用
按照原版的寫法,我們需要在 constraintlayout
中寫明約束條件,但由于我們把約束條件寫在了外部,那么直接調(diào)用即可
Modifier.layoutId
直接根據(jù)于約束集合中定義的名稱來應(yīng)用指定的約束條件;
@Composable fun DecoupleConstraintsLayout() { BoxWithConstraints { // 響應(yīng)式布局 val constraints = if (maxWidth < maxHeight) { decoupleConstraints(16.dp) } else { decoupleConstraints(160.dp) } // 把響應(yīng)式布局的if判斷作為參數(shù)傳入,約束布局即可按照對應(yīng)法則布置組件 ConstraintLayout(constraints) { // layoutId對應(yīng)我們在約束集合中配置的名稱! Button(onClick = { /*TODO*/ }, modifier = Modifier.layoutId("button")) { Text(text = "button") } Text(text = "text", modifier = Modifier.layoutId("text")) } } }
到此這篇關(guān)于Android ConstraintLayout約束布局使用實例介紹的文章就介紹到這了,更多相關(guān)Android ConstraintLayout內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
AndroidStudio 3.6 中 R.layout 找不到對應(yīng)的xml文件問題及解決方法
這篇文章主要介紹了AndroidStudio 3.6 中 R.layout 找不到對應(yīng)的xml文件問題,本文給出了解決方法對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android自定義view實現(xiàn)水波進(jìn)度條控件
這篇文章主要為大家詳細(xì)介紹了Android自定義view實現(xiàn)水波進(jìn)度條控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Android實現(xiàn)圖像灰度化、線性灰度變化和二值化處理方法
這篇文章主要介紹了Android實現(xiàn)圖像灰度化、線性灰度變化和二值化處理方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10Android利用Canvas標(biāo)點畫線并加入位移動畫(1)
這篇文章主要為大家詳細(xì)介紹了Android利用Canvas標(biāo)點畫線并加入位移動畫的第一篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09Android基于CountDownTimer實現(xiàn)倒計時功能
這篇文章主要介紹了Android基于CountDownTimer實現(xiàn)倒計時功能,簡單分析了基于CountDownTimer類實現(xiàn)倒計時功能的技巧,需要的朋友可以參考下2015-12-12