Constraints
是一个范围,用于衡量可测量的组成部分。如果未设置minheight,则它来自父级或以前的尺寸约束。如果它是0,那么你的Composable将在0和256.dp像素值之间进行测量,因为你需要将minHeight设置为与maxHeight相同的值,以便用固定高度而不是在一个范围内进行测量。假设内容为30.dp,并且以0-100.dp的范围进行测量,则从不为其分配100.dp的高度。
此外,设置placeable.place(0,yPos)和低于0的yPos将使其位于行中定义的位置之上。
最后,如果你不给父行分配hight,它将得到子行的高度,最大高度为
Box
使用布局修改器。
val itemWidth = 128
val itemHeight = 256
@Preview
@Composable
fun LayoutTest() {
Column(
modifier = Modifier.fillMaxSize()
) {
Row(
modifier = Modifier
.padding(top = 300.dp)
.fillMaxWidth()
.height(50.dp)
.border(2.dp, Color.Red)
) {
Box(
modifier = Modifier
.width(itemWidth.dp)
.layout { measurable, constraints ->
val placeable = measurable.measure(
constraints.copy(
minHeight = itemHeight.dp.roundToPx(),
maxHeight = itemHeight.dp.roundToPx()
)
)
layout(
placeable.width,
placeable.height
) {
placeable.placeRelative(0, 0)
}
}
.background(color = Color.Blue)
.border(2.dp, Color.Green)
)
Text(
text = "This is text filling the rest of the row",
modifier = Modifier
.weight(1f)
.padding(8.dp)
)
}
}
}
结果如下
蓝色框居中的原因是它不遵守父约束,因为父约束试图将其居中。您可以参考
this answer
有关更多详细信息,但一般来说,如果测量值不在父约束或先前修改器约束的范围内,则无论其大小,可组合的约束都是居中的。
为了克服这一点,你需要用以下方式抵消两者的差异
val userOffset = 0
placeable.placeRelative(
x = 0,
y = (constraints.maxHeight - placeable.height) / 2 + userOffset
)
将结果
如果为userOffset设置负值,蓝色框将放置在更靠近顶部的位置。