Jetpack Compose学习(14)——ConstraintLayout约束布局使用
原文地址: Jetpack Compose学习(14)——ConstraintLayout约束布局使用-Stars-One的杂货小窝
本文阅读之前,需要了解ConstraintLayout的使用!
本系列以往文章请查看此分类链接Jetpack compose学习
引入依赖
implementation("androidx.constraintlayout:constraintlayout-compose:1.1.0")
可在下方链接查看官方的最新版本
Constraintlayout | Jetpack | Android Developers
简单示例
在compose里的约束布局,需要先通过createRef()方法创建ref对象,之后通过Modifier.constrainAs()来进行对应的约束对齐,如下简单例子:
2个组件,图片和文本,文本与图片的top和bottom对齐,位于图片的右侧,间距16dp
注意: createRef(),createRefs(),Modifier.constrainAs()这个ConstraintLayoutScope作用域才能使用!下面的其他的方法也是如此,之后不再赘述!
ConstraintLayout(modifier= Modifier
.fillMaxWidth()
.height(100.dp)
.background(Color.Blue)) {//这里是ConstraintLayoutScope作用域
//createRef
//val portraitImageRef = remember {
// createRef()
//}
//val userNameTextRef = remember {
// createRef()
//}
//这个可以快速创建多个引用(但一次最多只能支持16个变量!)
val (portraitImageRef,userNameTextRef) = remember{ createRefs()}
Image(painter = painterResource(id = R.drawable.ic_auto), contentDescription =null ,modifier=Modifier.size(50.dp).constrainAs(portraitImageRef){
top.linkTo(parent.top)
start.linkTo(parent.start)
bottom.linkTo(parent.bottom)
})
Text("myname",modifier=Modifier.constrainAs(userNameTextRef){
top.linkTo(portraitImageRef.top)
//还可以设置margin和goneMargin,这里我只设置了margin属性
start.linkTo(portraitImageRef.end, margin = 16.dp)
bottom.linkTo(portraitImageRef.bottom)
})
}
constrainAs的函数体中,我们还可以设置当前组件的width和height属性,具体有下面几个选项
| Dimension Type | Description |
|---|---|
wrapContent() |
实际尺寸为根据内容自适应的尺寸 |
matchParent() |
实际尺寸为铺满整父组件的尺寸 |
fillToConstraints() |
实际尺寸为根据约束信息拉伸后的尺寸 |
preferredWrapContent() |
如果剩余空间大于根据内容自适应的尺寸时,实际尺寸为自适应的尺寸。如果剩余空间小于内容自适应的尺寸时,实际尺寸则为剩余空间的尺寸。 |
ratio (String) |
根据字符串计算实际尺寸所占比率,例如 "1:2" |
percent (Float) |
根据浮点数计算实际尺寸所占比率 |
value (Dp) |
将尺寸设置为固定值 |
preferredValue (Dp) |
如果剩余空间大于固定值时,实际尺寸为固定值。如果剩余空间小于固定值时,实际尺寸则为剩余空间的尺寸。 |
一个简单示例(某个组件占满剩余宽度):

ConstraintLayout(modifier= Modifier
.fillMaxWidth()
.height(100.dp)
.background(Color.Blue)) {
val (tv1Ref,tv2Ref) = remember{ createRefs()}
Text("一个字",modifier=Modifier.constrainAs(tv1Ref){
top.linkTo(parent.top)
start.linkTo(parent.start)
})
Text("右侧文本内容",modifier=Modifier.constrainAs(tv2Ref){
start.linkTo(tv1Ref.end)
end.linkTo(parent.end)
//占满剩余空间,实际等同于普通约束布局中,给width属性设置为0dp
width = Dimension.fillToConstraints
}.background(Color.Yellow))
}
除了上面几个start.linkTo,还有基线的对齐
baseline.linkTo(parent.baseline)
动态更改约束条件
除了上面说到的createRef方法,我们还可以通过Modifier.layoutId(id)和createRefFor(id)来联用进行创建ref对象
ConstraintSet对象就表明了当前的布局里的各组件的对齐关系,我们只需要构造ConstraintLayout的时候,传递此对象即可达到动态更新约束条件的效果!
下面是一个简单的示例:
private fun decoupledConstraints(margin: Dp): ConstraintSet {
return ConstraintSet {
val button = createRefFor("button")
val text = createRefFor("text")
constrain(button) {
top.linkTo(parent.top, margin = margin)
}
constrain(text) {
top.linkTo(button.bottom, margin)
}
}
}
@Composable
fun SettingPage(modifier: Modifier = Modifier) {
val constraints = decoupledConstraints(margin = 32.dp)
ConstraintLayout(constraints) {
Button(
onClick = { /* Do something */ }, modifier = Modifier.layoutId("button")
) {
Text("Button")
}
Text("Text", Modifier.layoutId("text"))
}
}
Barrier
平时在约束布局,不是很常用这个,一般用的GuideLine多些,不过也是记录下
这个需要依托存在的组件才能创建
ConstraintLayout{
val (tv1Ref,tv2Ref,iv1Ref) = remember{ createRefs()}
//创建位于组件右边的一个屏障
val barrier = createEndBarrier(tv1Ref,tv2Ref)
Text("一个字",modifier=Modifier.constrainAs(tv1Ref){
top.linkTo(parent.top)
start.linkTo(parent.start)
})
Text("十四个字",modifier=Modifier.constrainAs(tv2Ref){
top.linkTo(tv1Ref.bottom)
start.linkTo(tv1Ref.start)
})
//image始终位于2个文本的最右边(以最长文本为准)
Image(painter = painterResource(id = R.drawable.ic_auto), contentDescription =null,modifier=Modifier.size(50.dp).constrainAs(iv1Ref){
start.linkTo(barrier)
} )
}

createEndBarrier方法即在组件的右边位置创建屏障,除此之外还有其他3个方向的
createStartBarrier()createTopBarrier()createBottomBarrier()
GuideLine
引导线可以通过createGuidelineFromTop()方法直接创建,个人一般用此来进行百分比宽度等划分,然后让组件占满
于上面一样,还有其他方向,这里就不补充了,就是换个名字,代码提示直接可以搜到了
方法可接受一个0-1f范围之间的百分比或者固定的偏移量dp,如下面例子:
val guide = createGuidelineFromTop(0.2f)
val guide = createGuidelineFromTop(20.dp)
一个完整使用示例:

ConstraintLayout(modifier= Modifier
.fillMaxSize()
.background(Color.Blue)) {
val (tv2Ref) = remember { createRefs() }
val guide = createGuidelineFromTop(0.2f)
Text("底下占满",modifier= Modifier
.constrainAs(tv2Ref) {
top.linkTo(guide)
bottom.linkTo(parent.bottom)
width = Dimension.matchParent
height = Dimension.fillToConstraints
}
.background(Color.Yellow))
}
Chain
熟悉约束布局使用都知道这个了,有水平或垂直2种,然后ChainStyle类型有3种,这里不赘述了
createVerticalChain()createHorizontalChain()
createVerticalChain(imageOneRef, imageTwoRef, chainStyle = ChainStyle.Spread)
参考
Jetpack Compose学习(14)——ConstraintLayout约束布局使用的更多相关文章
- Jetpack Compose学习(5)——从登录页美化开始学习布局组件使用
原文:Jetpack Compose学习(5)--从登录页美化开始学习布局组件使用 | Stars-One的杂货小窝 本篇主要讲解常用的布局,会与原生Android的布局控件进行对比说明,请确保了解A ...
- Jetpack Compose学习(3)——图标(Icon) 按钮(Button) 输入框(TextField) 的使用
原文地址: Jetpack Compose学习(3)--图标(Icon) 按钮(Button) 输入框(TextField) 的使用 | Stars-One的杂货小窝 本篇分别对常用的组件:图标(Ic ...
- Jetpack Compose学习(1)——从登录页开始入门
原文地址:Jetpack Compose学习(1)--从登录页开始入门 | Stars-One的杂货小窝 Jetpack Compose UI在前几天出了1.0正式版,之前一直还在观望,终于是出了正式 ...
- Jetpack Compose学习(2)——文本(Text)的使用
原文: Jetpack Compose学习(2)--文本(Text)的使用 | Stars-One的杂货小窝 对于开发来说,文字最为基础的组件,我们先从这两个使用开始吧 本篇涉及到Kotlin和DSL ...
- Jetpack Compose学习(4)——Image(图片)使用及Coil图片异步加载库使用
原文地址 Jetpack Compose学习(4)--Image(图片)使用及Coil图片异步加载库使用 | Stars-One的杂货小窝 本篇讲解下关于Image的使用及使用Coil开源库异步加载网 ...
- Jetpack Compose学习(6)——关于Modifier的妙用
原文: Jetpack Compose学习(6)--关于Modifier的妙用 | Stars-One的杂货小窝 之前学习记录中也是陆陆续续地将常用的Modifier的方法穿插进去了,本期就来详细的讲 ...
- Jetpack Compose学习(7)——MD样式架构组件Scaffold及导航底部菜单
Jetpack Compose学习(7)--MD样式架构组件Scaffold及导航底部菜单 | Stars-One的杂货小窝 Compose给我们提供了一个Material Design样式的首页组件 ...
- Jetpack Compose学习(9)——Compose中的列表控件(LazyRow和LazyColumn)
原文:Jetpack Compose学习(9)--Compose中的列表控件(LazyRow和LazyColumn) - Stars-One的杂货小窝 经过前面的学习,大致上已掌握了compose的基 ...
- Jetpack Compose学习(11)——Navigation页面导航的使用
原文:Jetpack Compose学习(11)--Navigation页面导航的使用 - Stars-One的杂货小窝 在Android原生的View开发中的,也是有Navigation,原生我之后 ...
- 使用ConstraintLayout(约束布局)构建响应式UI
使用ConstraintLayout(约束布局)构建响应式UI 转 https://www.300168.com/yidong/show-2740.html 核心提示:ConstraintLa ...
随机推荐
- SQL Server – Work with JSON
前言 JSON 是一个很好的格式, array, object 就能表达一个表格了. 如果想保存一些结构格式, 又不想用表格这么大费周章的话, JSON 会是很好选择. 比如我用它来记入 Audit ...
- AtCoder Regular Contest 182(A B C)
原来第二题比第一题简单吗 A.Chmax Rush! \(\texttt{Diff 1110}\) 给定三个序列 \(S,P,V\),其中 \(S\) 的长度为 \(N\),\(P,V\) 的长度为 ...
- Hive 2.3.2安装
一.安装mysql 安装MySQL服务器端和MySQL客户端: •安装: – yum install mysql – yum install mysql-server •启动: – /etc/init ...
- balance_dirty_pages_ratelimited分析
balance_dirty_pages_ratelimited分析 nr_dirtied_pause:当前task的脏页门限: dirty_exceeded:全局的脏页数超过门限或者该bdi的脏页数超 ...
- Failed to connect to github.com port 443: Connection refused问题解决
解决办法: 1.找到github的ip地址:查找链接 2.找到本地的hosts文件.我的hosts文件路劲为:C:\Windows\System32\drivers\etc 3.在hosts文件最后添 ...
- PC软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
前言 国庆假期各种活动比较多,直到上班才有时间来更新文章~ 不过这两天我还是做了个小玩意(Clipify),起因是想给之前开发来自己用的简单视频剪辑工具 QuickCutSharp 加个功能,不过这个 ...
- 数据库运维实操优质文章分享(含Oracle、MySQL等) | 2023年6月刊
本文为大家整理了墨天轮数据社区2023年6月发布的优质技术文章/文档,主题涵盖Oracle.MySQL.PostgreSQL等数据库的安装配置.故障处理.性能优化等日常实践操作,以及常用脚本.注意事项 ...
- kotlin函数和Lambda表达式——>函数
函数: 1.函数声明 kotlin中的函数使用fun关键字声明: fun double(x: Int): Int { return 2 * x } 2.函数用法 调用函数使用传统的方法: val re ...
- TrueNAS关闭5357端口
sockstat -4l 查看监听5357端口进程的ID. 用kill -9 PID 干掉进程.
- 在Windows环境下使用AMD显卡运行Stable Diffusion
现在用的电脑是 21 年配的,当时并没有 AI 相关的需求,各种各样的原因吧,抉择后选择了 AMD 的显卡,但在 2024 年的今天,使用 AI 进行一些工作已不再是什么罕见的需求,所以我也想尝试一下 ...