一,简介

Jetpack compose中没有提供ConstraintLayout支持,所以需要添加下面的依赖来导入。

// build.gradle
implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-alpha07"

在Compose中,ConstraintLayout需要通过DSL来使用。

  • createRefs()或者createRef()创建references,ConstraintLayout中的每一个composable都需要创建(guidelines,barriers不需要)
  • constrainAs可以把reference当作参数,然后在lambda中设置约束关系
  • linkTo来表明约束关系
  • parent是已经存在的reference,标识ConstraintLayout本身

二,示例

效果一:约束Button和Text的关系

@Composable
fun ConstraintLayoutContent() {
ConstraintLayout {
// 创建references
val (button, text) = createRefs() Button(
onClick = {},
// reference "button"相当于原来的id
// 约束在constraintlayout的top,margin为16dp
modifier = Modifier.constrainAs(button) {
top.linkTo(parent.top, margin = 16.dp)
}
) {
Text("Button")
}
// reference "text"
// 约束Text在Button的下面,margin为16dp
Text("Text", Modifier.constrainAs(text) {
top.linkTo(button.bottom, margin = 16.dp)
})
}
}

效果二:Text在parent中居中显示

@Composable
fun ConstraintLayoutContent() {
ConstraintLayout {
// 和上面的例子一致
... // reference "text"
// 约束Text在Button的下面,margin为16dp
Text("Text", Modifier.constrainAs(text) {
top.linkTo(button.bottom, margin = 16.dp)
// Text在parent居中
// ConstraintLayout默认是wrap_content的
centerHorizontallyTo(parent)
})
}
}

效果三:创建Barrier

@Composable
fun ConstraintLayoutContent() {
ConstraintLayout {
// 创建references
val (button1, button2, text) = createRefs() Button(
onClick = {},
modifier = Modifier.constrainAs(button1) {
top.linkTo(parent.top, margin = 16.dp)
}
) {
Text("Button1")
}
// Text显示在Button1的下方,Text的中线和Button1的end对齐
Text("Text", Modifier.constrainAs(text) {
top.linkTo(button1.bottom, margin = 16.dp)
centerAround(button1.end)
})
// 根据button1和text的end(取其长)创建barrier
val barrier = createEndBarrier(button1, text)
// Button2显示在barrier开始处
Button(
onClick = {},
modifier = Modifier.constrainAs(button2) {
top.linkTo(parent.top, margin = 16.dp)
start.linkTo(barrier)
}
) {
Text("Button2")
}
}
}

效果四:创建guideline

@Composable
fun LargeConstraintLayout() {
ConstraintLayout {
val text = createRef()
// 设置为屏幕宽度的一半
val guideline = createGuidelineFromStart(fraction = 0.5f)
Text(
"This is a very very very very very very long text",
Modifier.constrainAs(text) {
linkTo(start = guideline, end = parent.end)
}
)
}
}

效果五:设置Text的宽度为wrap_content

@Composable
fun LargeConstraintLayout() {
ConstraintLayout {
val text = createRef() val guideline = createGuidelineFromStart(fraction = 0.5f)
Text(
"This is a very very very very very very long text",
Modifier.constrainAs(text) {
linkTo(start = guideline, end = parent.end)
width = Dimension.preferredWrapContent
}
)
}
}

除了preferredWrapContent,还有其他几个设置项

preferredWrapContent  the layout is wrap content, subject to the constraints in that dimension.
wrapContent  the layout is wrap content even if the constraints would not allow it.
fillToConstraints the layout will expand to fill the space defined by its constraints in that dimension.
preferredValue the layout is a fixed dp value, subject to the constraints in that dimension.
value  the layout is a fixed dp value, regardless of the constraints in that dimension.

也可以设置在固定值范围内,如

width = Dimension.preferredWrapContent.atLeast(100.dp)

效果六:横屏和纵屏的情况下,margin不一样

在ConstraintSet中通过createRefFor创建reference,创建不同的ConstraintSet,传递给ConstraintLayout。

@Composable
fun DecoupledConstraintLayout() {
BoxWithConstraints {
val constraints = if (maxWidth < maxHeight) {
// 竖屏
decoupledConstraints(margin = 16.dp)
} else {
// 横屏
decoupledConstraints(margin = 32.dp)
} ConstraintLayout(constraints) {
Button(
onClick = {},
modifier = Modifier.layoutId("button")
) {
Text("Button")
}
Text("Text", Modifier.layoutId("text"))
}
}
} 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)
}
}
}

更多内容请参看Layouts in Jetpack Compose (google.cn)

Jetpack compose学习笔记之ConstraintLayout(布局)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. 微信小程序开发:学习笔记[4]——样式布局

    微信小程序开发:学习笔记[4]——样式布局 Flex布局 新的布局方式 在小程序开发中,我们需要考虑各种尺寸终端设备上的适配.在传统网页开发,我们用的是盒模型,通过display:inline | b ...

  9. amazeui学习笔记--css(布局相关1)--网格Grid

    amazeui学习笔记--css(布局相关1)--网格Grid 一.总结 基本使用 1.div+class布局:amaze里面采取的就是div+class的布局方式  <div class=&q ...

  10. amazeui学习笔记--css(布局相关3)--辅助类Utility

    amazeui学习笔记--css(布局相关3)--辅助类Utility 一.总结 1.元素清除浮动: 添加 am-cf 这个 class 即可 2.水平滚动: .am-scrollable-horiz ...

随机推荐

  1. 【数据库数据恢复】Oracle数据库数据恢复案例

    数据库故障:Oracle数据库的ASM磁盘组掉线,ASM实例不能挂载.管理员尝试修复数据库但是没有成功. 数据库数据恢复方案:数据库数据恢复工程师通过分析组成ASM磁盘组的磁盘底层数据,将ASM元数据 ...

  2. 【记录】 iSCSI服务器的搭建与使用[Debian]

    序言 更换系统后需要一个网络文件存储用于备份文件,本想用NFS多方便,但是timeshift不支持网络存储,备份路径必须是一个块存储设备, 但是你还必须分好文件系统,这不是多此一举???反正我只用rs ...

  3. java中取整数绝对值_Java之——位运算求整数绝对值通过下面的位运算可以得到一个整数的绝对值

    public int abs( int a ) {return (a + (a >> 31)) ^ (a >> 31) ;//前半部分-1或+0,后半部分取反 } a为正数的情 ...

  4. 一、MySQL 函数

    1.MySQL 字符串函数 函数 描述 实例 结果展示 说明 REPLACE(s,s1,s2) 将字符串s2代替字符串s中的字符串s1 SELECT REPLACE(ccc.contract_no,& ...

  5. VS2017创建Linux项目实现远程GDB调试

    vs2017新增linux for C++的模块,尝试安装了一下环境. 首先,安装VS2017,安装时注意选择以下模块: 安装完成后,需要配置Linux服务端的部分,我的配置过程如下: 第一步,安装V ...

  6. python面向对象--类的刨析

    编程日常::::#编程就是程序员用特定的语法加数据结构加算法在计算机上执行过程,方式有很多种,最常用的就是面向对象编程和面向过程编程#设计思路一开始解决一个大问题,然后把大问题分解成小问题,一步步解决 ...

  7. rust crm 镜像源管理

    一.下载crm cargo install crm https://github.com/wtklbm/crm 二.命令 # 在终端执行 # # NOTE: # - [args] 表示 args 是一 ...

  8. .NET 7介绍及环境准备

    环境要求 VS2022 17.4+

  9. Docker info 查看报错 WARNING: No swap limit support 解决

    docker可以通过启动命令来限制容器可以使用的最大物理内存和swap,但是通常在使用这些命令的时候经常会碰到"WARNING: No swap limit support"警告 ...

  10. Godot的场景树

    在Godot中,一个游戏的启动大致流程如下: 简而言之,Godot的main启动一个进程,加载所需的驱动设备(如渲染设备:GL/GLES/Vulkan等).音频设备,输入控制器设备等等:然后进入主循环 ...