Jetpack Compose 中的约束宽度/约束高度

问题描述 投票:0回答:3

如何在 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"

?

android android-constraintlayout android-jetpack-compose
3个回答
0
投票

你可以使用

width = Dimension.fillToConstraints

0
投票

使用

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
                   )
           }

了解更多信息 - https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/Dimension.Companion#preferredWrapContent()


0
投票

在约束布局中 这是各种宽度和高度模式的示例。

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)
将分别默认为 wrapconstrainedfilled。 但至少会强制执行附加约束,等等。

© www.soinside.com 2019 - 2024. All rights reserved.