gpt4 book ai didi

android - 重叠两个 Box jetpack 组合

转载 作者:行者123 更新时间:2023-12-04 23:52:43 26 4
gpt4 key购买 nike

我试图重叠两个 Box或者最好使用Row在这个案子上。
我的设计是一个Row与另一个重叠,我已将其包裹在 Column 上, 那是对的吗?
这就是设计,我想要的是顶部的矩形与下面的矩形大小相同,然后将其移动一些像素,如您在图像中看到的那样,但它们应该具有相同的宽度但不是相同的高度。
enter image description here
如果层次结构是可以的:

Column 
Box (the one of the top)
Row
Box (the one of the bottom)
Row (inside there is text and it's all the same align)

最佳答案

几天前我遇到过这个问题,我使用 ConstraintLayout 解决了它.
我必须做的是:

  • 添加 implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-beta02"build.gradle
  • 包裹每个 BoxConstraintLayout { .. }
  • 里面每个Box添加 Modifier.constrainAs对齐 Top Bottom Start End如你所愿。
  • 如果您希望第一个框相同 width作为第二个没有硬编码 dps你应该使用 width = Dimension.fillToConstraints

  • fillToConstraints - the layout will expand to fill the space defined by its constraints in that dimension.


    没有硬编码的基本示例:
    ConstraintLayout() {
    val (title, description) = createRefs()
    Box(
    modifier = Modifier
    .padding(start = 28.dp)
    .background(color = Red)
    .padding(
    horizontal = 16.dp,
    )
    .constrainAs(title) {
    top.linkTo(parent.top)
    start.linkTo(parent.start)
    end.linkTo(parent.end)
    width = Dimension.fillToConstraints
    }
    ) {
    Text(text = "Hello World")
    }

    Box(
    modifier = Modifier
    .padding(end = 4.dp)
    .background(Color.Magenta)
    .padding(bottom = 5.dp, start = 8.dp, end = 16.dp, top = 4.dp)
    .constrainAs(description) {
    top.linkTo(title.top, margin = 16.dp)
    start.linkTo(parent.start)
    end.linkTo(parent.end)
    bottom.linkTo(parent.bottom)
    }
    ) {
    Text(text = "Skizo-ozᴉʞS rules")
    }
    }
    现在你必须玩 padding根据您的 UI 并对其进行调整,结果是这样的:
    enter image description here

    关于android - 重叠两个 Box jetpack 组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70799640/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com