原文地址: Jetpack Compose学习(14)——ConstraintLayout约束布局使用-Stars-One的杂货小窝

本文阅读之前,需要了解ConstraintLayout的使用!

各位可查阅我的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约束布局使用的更多相关文章

  1. Jetpack Compose学习(5)——从登录页美化开始学习布局组件使用

    原文:Jetpack Compose学习(5)--从登录页美化开始学习布局组件使用 | Stars-One的杂货小窝 本篇主要讲解常用的布局,会与原生Android的布局控件进行对比说明,请确保了解A ...

  2. Jetpack Compose学习(3)——图标(Icon) 按钮(Button) 输入框(TextField) 的使用

    原文地址: Jetpack Compose学习(3)--图标(Icon) 按钮(Button) 输入框(TextField) 的使用 | Stars-One的杂货小窝 本篇分别对常用的组件:图标(Ic ...

  3. Jetpack Compose学习(1)——从登录页开始入门

    原文地址:Jetpack Compose学习(1)--从登录页开始入门 | Stars-One的杂货小窝 Jetpack Compose UI在前几天出了1.0正式版,之前一直还在观望,终于是出了正式 ...

  4. Jetpack Compose学习(2)——文本(Text)的使用

    原文: Jetpack Compose学习(2)--文本(Text)的使用 | Stars-One的杂货小窝 对于开发来说,文字最为基础的组件,我们先从这两个使用开始吧 本篇涉及到Kotlin和DSL ...

  5. Jetpack Compose学习(4)——Image(图片)使用及Coil图片异步加载库使用

    原文地址 Jetpack Compose学习(4)--Image(图片)使用及Coil图片异步加载库使用 | Stars-One的杂货小窝 本篇讲解下关于Image的使用及使用Coil开源库异步加载网 ...

  6. Jetpack Compose学习(6)——关于Modifier的妙用

    原文: Jetpack Compose学习(6)--关于Modifier的妙用 | Stars-One的杂货小窝 之前学习记录中也是陆陆续续地将常用的Modifier的方法穿插进去了,本期就来详细的讲 ...

  7. Jetpack Compose学习(7)——MD样式架构组件Scaffold及导航底部菜单

    Jetpack Compose学习(7)--MD样式架构组件Scaffold及导航底部菜单 | Stars-One的杂货小窝 Compose给我们提供了一个Material Design样式的首页组件 ...

  8. Jetpack Compose学习(9)——Compose中的列表控件(LazyRow和LazyColumn)

    原文:Jetpack Compose学习(9)--Compose中的列表控件(LazyRow和LazyColumn) - Stars-One的杂货小窝 经过前面的学习,大致上已掌握了compose的基 ...

  9. Jetpack Compose学习(11)——Navigation页面导航的使用

    原文:Jetpack Compose学习(11)--Navigation页面导航的使用 - Stars-One的杂货小窝 在Android原生的View开发中的,也是有Navigation,原生我之后 ...

  10. 使用ConstraintLayout(约束布局)构建响应式UI

    使用ConstraintLayout(约束布局)构建响应式UI 转 https://www.300168.com/yidong/show-2740.html     核心提示:ConstraintLa ...

随机推荐

  1. EF Core – QueryFilter & Interception

    主要参考 Global Query Filters Interceptors QueryFilter QueryFilter 就是默认过滤, 非常适合用来做 Soft Delete builder.H ...

  2. sicp每日一题[2.13-2.16]

    Exercise 2.13 Show that under the assumption of small percentage tolerances there is a simple formul ...

  3. redisson内存泄漏问题排查

    问题描述 最近生产有个服务突然出现频繁告警,接口P99响应时间变长,运维同学观察到相应的pod cpu飙升,内存占用很高. cpu升高问题排查是老生常谈的话题了,一般可以使用top -p pid -H ...

  4. Linux中的一些命令

    1.新增新用户lili,不允许登录系统,用户ID为3000===useradd -u 3000 -s /sbin/nologin lili2.循环创建目录 /www/wwwroot/html/test ...

  5. 共124篇!墨天轮“高可用架构”干货文档分享(含Oracle、MySQL、PG)

    大家期待的高可用篇来啦!在上期<墨天轮高分技术文档分享-Oracle升级迁移篇>中大家对数据库高可用架构相关文档呼声较高,这不就来啦! 数据库的高可用架构能够在发生宕机或意外中断等故障时起 ...

  6. 前端面试题axaios携带 cookies

    配置 axios.default.widthCredentials = true;

  7. 新建数据库 phpStudy

    官网:https://www.xp.cn/下载phpStudy : 环境配置:1. 下载MySQL8.0.12 2. 安装HeidiSQL11.0 开启 MySQL8.0.12 修改MySQL的密码: ...

  8. c#传统读取配置文件

    using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; namespace C ...

  9. 0404-Tensor的持久化和向量化

    0404-Tensor的持久化和向量化作 目录 一.持久化 1.1 保存模型 1.2 加载模型 二.向量化 三.注意事项 四.第四章总结 pytorch完整教程目录:https://www.cnblo ...

  10. 云原生周刊:Helm Charts 深入探究 | 2024.3.11

    开源项目推荐 Glasskube Glasskube 提供了一个用于 Kubernetes 的缺失的包管理器.它具有图形用户界面(GUI)和命令行界面(CLI).Glasskube 包是具备依赖感知. ...