android 使用约束布局合成Jetpack中的约束宽度/约束高度

pftdvrlh  于 2023-03-16  发布在  Android
关注(0)|答案(3)|浏览(141)

如何在合成中设置约束宽度/高度?

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"

wko9yo5t

wko9yo5t1#

您可以使用

width = Dimension.fillToConstraints
dkqlctbz

dkqlctbz2#

使用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:Kotlin#首选 Package 内容()

n8ghc7c1

n8ghc7c13#

在constraintLayout中下面是各种宽度和高度模式的示例。

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)

preferredWrapContentfillToConstraintspreferredValue(x.dp)将分别默认为 wrapconstrainedfilled。但将至少强制附加约束,等等。

相关问题