简体   繁体   English

Jetpack Compose 中是否有类似 ConstraintLayout 的“dimensionRatio”属性?

[英]Is there something like ConstraintLayout's "dimensionRatio" property in Jetpack Compose?

I wanna constraint the Image's start to parent's start, top to top, end to end when its width\/height is 4\/3, just like app:layout_constraintDimensionRatio="H,3:4"<\/code> in Android Xml.当图像的宽度\/高度为 4\/3 时,我想将图像的开始限制为父的开始,从上到下,从端到端,就像 Android Xml 中的app:layout_constraintDimensionRatio="H,3:4"<\/code> 。

Here's my code below :下面是我的代码:

ConstraintLayout(
        modifier = Modifier
            .wrapContentHeight()
            .width(162.dp)
            .clip(RoundedCornerShape(10.dp))
            .background(color = Color.White)
            .clickable {
                //do something
            }
    ) {
        val (coverImg, title, status, date) = createRefs()

        Image(
            "some ignored properties",
            modifier = Modifier
                .constrainAs(coverImg) {
                    linkTo(start = parent.start, end = parent.end)
                    top.linkTo(parent.top)
                    width = Dimension.fillToConstraints
                }
                .height(102.dp)//I don't want to specify its height
        )
        
        Text(...)
        AnyOtherLayout(...)
}

You can use aspectRatio modifier in jetpack compose.您可以在 jetpack compose 中使用 aspectRatio 修饰符。

modifier = Modifier.aspectRatio(0.75f)

It takes two parameters first one is a single float value that represents that aspect ratio.它需要两个参数,第一个是表示该纵横比的单个浮点值。 Like If you want to use 3:4 you have to input 3/4f or 3/4 = .75f.就像如果你想使用 3:4 你必须输入 3/4f 或 3/4 = .75f。

2nd one is optional by default it's false.第二个是可选的,默认为 false。 If you send true it will consider Constraints.maxHeight first.如果您发送 true,它将首先考虑 Constraints.maxHeight。

matchHeightConstraintsFirst: Boolean = false

You can use the aspectRatio modifier:您可以使用aspectRatio修饰符:

    Image(
        painter = painterResource(id = R.drawable.xxx),
        "some ignored properties",
        contentScale = ContentScale.FillBounds,
        modifier = Modifier
            .constrainAs(coverImg) {
                linkTo(start = parent.start, end = parent.end)
                top.linkTo(parent.top)
                width = Dimension.fillToConstraints
            }
            .aspectRatio(4f/3f)
    )

The problem with using Modifier.aspectRatio is that it doesn't seem to be taken into account when using other constraints.使用Modifier.aspectRatio的问题在于,在使用其他约束时似乎没有考虑到它。 Aspect ratios are actually supported by ConstraintLayout itself纵横比实际上由 ConstraintLayout 本身支持

Take this layout for example, we constrain one dimension as a ratio of 16:9以这种布局为例,我们将一个维度限制为 16:9 的比例

ConstraintLayout(modifier = Modifier.fillMaxSize()) {

    val (box) = createRefs()
    val guidelineBottom = createGuidelineFromBottom(0.1f)

    Box(modifier = Modifier
        .background(Color.Blue)
        .constrainAs(box) {
            linkTo(parent.start, parent.end, startMargin = 32.dp, endMargin = 32.dp)
            linkTo(parent.top, guidelineBottom)

            width = Dimension.ratio("16:9")
            height = Dimension.fillToConstraints
        }
    )
}

That gives us this这给了我们这个

在此处输入图像描述

If we then change the offset of guidelineBottom to decrease the available height we end up with this如果我们然后更改 guidelineBottom 的偏移量以减少可用高度,我们最终会得到这个

val guidelineBottom = createGuidelineFromBottom(0.9f)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetpack compose 中的 ConstraintLayout 问题 - Issue with ConstraintLayout in Jetpack compose ConstraintLayout 中的 ConstraintLayout 在 Jetpack Compose 中不起作用 - ConstraintLayout inside ConstraintLayout not working in Jetpack Compose 在 LazyColumn jetpack compose 中有类似 swiperefreshlayout 的东西可以拉动刷新 - There is something similar like swiperefreshlayout to pull to refresh in the LazyColumn jetpack compose 如何将 Jetpack Compose ConstraintLayout 与 Column 一起使用 - How to use Jetpack Compose ConstraintLayout together with Column 在 ConstraintLayout 中使用 Jetpack Compose 垂直滚动组件 - Use Jetpack Compose vertically scrollable component in ConstraintLayout 如何使用 ConstraintLayout 在 DimensionRatio 上设置 maxHeight? - How to set maxHeight over DimensionRatio with ConstraintLayout? 我应该在 Android Jetpack Compose 中使用 ConstraintLayout 以获得更好的性能吗? - Should I use ConstraintLayout in Android Jetpack Compose for a better performance? ConstraintLayout 是否仍然优于 Jetpack Compose 中的嵌套列和行? - Is ConstraintLayout still preferred over nested Column & Row in Jetpack Compose? ConstraintLayout 与 Jetpack Compose 中的 LazyRow 不兼容 - ConstraintLayout doesn't quite work with LazyRow in Jetpack Compose Jetpack Compose 与 Coroutine 的 StateFlow - Jetpack Compose with Coroutine's StateFlow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM