網(wǎng)站首頁 編程語言 正文
基本結(jié)構(gòu)
約束結(jié)構(gòu)原理:將一個組件與約束布局關(guān)聯(lián)后,通過 modifier 來指定組件位置
導(dǎo)入 compose 約束布局包
打開 build.gradle(:app)
文件
在依賴中添加以下這一行,之后點(diǎn)擊頂部的 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作為參照點(diǎn)進(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")) } } }
原文鏈接:https://blog.csdn.net/delete_you/article/details/127062854
相關(guān)推薦
- 2022-05-16 批處理copy命令的具體使用_DOS/BAT
- 2023-12-21 uniapp 清除文件緩存
- 2022-11-11 Navicat?Premium自定義?sql?標(biāo)簽的創(chuàng)建方式_數(shù)據(jù)庫其它
- 2022-07-02 python之NAN和INF值處理方式_python
- 2022-07-12 mongoDB復(fù)雜查詢實(shí)例(嵌套多個數(shù)組和正則表達(dá)式使用)
- 2022-09-13 go語言中切片Slice與數(shù)組Array對比以及panic:?runtime?error:?inde
- 2022-01-15 let、var、const區(qū)別
- 2022-10-01 詳解Python變量與注釋高級用法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支