android-jetpack-compose - 行内的 fillMaxHeight()
问题描述
我的问题与这个有关:如何在 Jetpack Compose 中实现这种布局
我有这个代码:
@Composable
fun TestUi() {
Row {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.background(color = Color.Yellow)
.fillMaxHeight()
) {
CircularProgressIndicator()
}
Image(imageVector = vectorResource(id = R.drawable.ic_launcher_background))
}
}
我希望得到这个:
但我得到了这个:
如何让 Box 填充所有可用高度而不影响 Row 的高度?
我知道我可以使用 ConstraintLayout 来解决这个问题,但是对于这样一个简单的用例来说似乎太多了。
解决方案
看看 Layout Composable 或 Modifier。您可以先测量定义元素,然后为从属元素提供修改后的约束。如果您想将其用作修饰符,则应为列表添加大小检查。
Layout(content = {
Box(modifier = Modifier
.size(width = 30.dp, height = 50.dp)
.background(Color.Green))
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.background(color = Color.Yellow)
.fillMaxHeight()
) {
CircularProgressIndicator()
}
}) { measurables: List<Measurable>, constraints: Constraints ->
val heightDef = measurables[0].measure(constraints)
val other = measurables[1].measure(
constraints.copy(
maxHeight = heightDef.height,
maxWidth = constraints.maxWidth - heightDef.width)
)
layout(
width = heightDef.width + other.width,
height = heightDef.height
) {
other.placeRelative(0,0)
heightDef.placeRelative(other.width,0)
}
}
推荐阅读
- ruby-on-rails - 比较多个 Rails I18n YAML 文件以使它们匹配?
- python - 上采样(分解)将季度数据汇总到月度数据
- docker - 将应用程序部署到 OpenShift 的多种选择
- angularjs - 如何使用 ui-grid 应用 pagination.js
- ios - Swift 4 Firebase Auth 自定义错误代码
- python - 添加或读取时处理数据
- bash - bash 脚本 - 如何使用作为变量调用的函数的结果
- ruby-on-rails - Active Record 添加普通 sql 索引失败,区分大小写
- c# - 如何捏合和缩放或平移 ARCore 摄像头。Unity3d
- c++ - 如何包装多级原始指针以赋予它容器语义