如何在 Compose 中设置约束宽度/高度?
Text("Test", Modifier.constrainAs(text) {
linkTo(
start = parent.start,
top = button.bottom,
end = parent.end,
bottom = parent.bottom,
topMargin = 16.dp,
horizontalBias = 0f
)
width = Dimension.wrapContent // this is default, can be removed
// now I need to set constrainedWidth somehow like in XML app:layout_constrainedWidth
})
width = Dimension.preferredWrapContent
mb 这个等于
android:layout_width="wrap_content"
app:layout_constrainedWidth="true"
?
你可以使用
width = Dimension.fillToConstraints
使用
Dimension.preferredWrapContent
对我有用:
modifier = Modifier.constrainAs(content) {
width = Dimension.preferredWrapContent
linkTo(
start = parent.start,
top = button.bottom,
end = parent.end,
bottom = parent.bottom,
topMargin = 16.dp,
horizontalBias = 0f
)
}
在约束布局中 这是各种宽度和高度模式的示例。
width = Dimension.matchParent
width = Dimension.value(23.dp)
width = Dimension.fillToConstraints
width = Dimension.fillToConstraints.atLeast(32.dp)
width = Dimension.fillToConstraints.atMost(125.dp)
width = Dimension.fillToConstraints.atLeast(32.dp).atMost(125.dp)
width = Dimension.fillToConstraints.atLeastWrapContent
width = Dimension.fillToConstraints.atLeastWrapContent.atMost(123.dp)
width = Dimension.fillToConstraints.atMostWrapContent
width = Dimension.fillToConstraints.atMostWrapContent.atLeast(23.dp)
width = Dimension.preferredWrapContent.atLeast(32.dp)
width = Dimension.preferredWrapContent.atMost(125.dp)
width = Dimension.preferredWrapContent.atLeast(32.dp).atMost(125.dp)
width = Dimension.preferredWrapContent.atLeastWrapContent
width = Dimension.preferredWrapContent.atMostWrapContent
width = Dimension.preferredValue(23.dp).atLeastWrapContent
width = Dimension.preferredValue(23.dp).atLeast(23.dp)
width = Dimension.preferredValue(23.dp)
width = Dimension.ratio("1:1")
width = Dimension.wrapContent
width = Dimension.percent(0.6f)
preferredWrapContent
, fillToConstraints
, preferredValue(x.dp)
将分别默认为 wrap、constrained 或 filled。
但至少会强制执行附加约束,等等。