Layout

正如您所看到的布局,我希望将较小的框覆盖在较大框的边缘上. 我试着自己实现它,但我所能做的就是将较小的覆盖在较大的边缘上,而不是在边缘之上.

我试图这样实现它,但我想我只是没有在偏移量修饰符中给出正确的参数.

Box {
        Card(
            modifier= Modifier
                .fillMaxWidth()
                .height(300.dp)
        ){

        }
        Box(
            modifier = Modifier
                .width(100.dp)
                .height(100.dp)
                .background(Color.Blue)
                .align(Alignment.TopStart)
                .offset(x=0.dp ,y=40.dp)
        )
    }

推荐答案

作为Box的替代方法,您可以使用自定义布局

@Composable
fun OverlappingBoxes(
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit,
) {
    Layout(
        modifier = modifier,
        content = content,
    ) { measurables, constraints ->
        val largeBox = measurables[0]
        val smallBox = measurables[1]
        val looseConstraints = constraints.copy(
            minWidth = 0,
            minHeight = 0,
        )
        val largePlaceable = largeBox.measure(looseConstraints)
        val smallPlaceable = smallBox.measure(looseConstraints)
        layout(
            width = constraints.maxWidth,
            height = largePlaceable.height + smallPlaceable.height / 2,
        ) {
            largePlaceable.placeRelative(
                x = 0,
                y = 0,
            )
            smallPlaceable.placeRelative(
                x = - smallPlaceable.width / 2, // overlap by exactly half the sise of second box x axis wise
                y = largePlaceable.height / 2 - smallPlaceable.height / 2 //center with y axis
            )
        }
    }
}

@Composable
fun BoxOverlap(
    modifier: Modifier = Modifier,
) {
    OverlappingBoxes(modifier = modifier.size(300.dp)) {
        Box(
            modifier = Modifier
                .size(300.dp)
                .background(Color.Green)
        ) {
        }

        Box(
            modifier = Modifier
                .size(100.dp)
                .clip(RoundedCornerShape(16.dp))
                .background(Color.Blue)
        ) {

        }
    }
}

enter image description here

如果您希望第二个框偏移基于百分比

val percentageFromTop = 0.3f // 30% from the top
val yOffset = (largePlaceable.height * percentageFromTop).roundToInt()
smallPlaceable.placeRelative(x = - smallPlaceable.width / 2,y = yOffset - smallPlaceable.height / 2
            )

在使用约束布局的情况下还有其他答案Overlap two Box jetpack compose.但是,如果您想使用百分比,您可以使用自定义布局

Android相关问答推荐

Android Compose Lazy柱形实时UI更改

Android意图过滤器不限制应用程序仅处理YouTube链接

NativeScript在`ns run android`上重复Kotlin类

Android和Rust,OpenSSL交叉编译在ARM V7上链接失败

使用Kotlin/Compose与Java/XML指南的比较

每次重启Android时预填入Room数据库

如何解决Gradle构建错误:java.lang.NoSuchMethodError

如何在喷气背包中绕过集装箱

更改选定的切换轨道 colored颜色

看不到选项菜单栏

从包含的(复合)模块导入 Kotlin 类时,Android 应用程序模块的 build.gradle.kts 未解决的引用错误

错误:参数的类型必须是用@Entity注释的类或其集合/array. java.lang.String tocd);

SQLite Kotlin 问题 - 没有数据库

在段的中心绘制饼图(甜甜圈图)的图例

Android Studio 复制类 kotlin

如何添加到先前预填充的 Room 数据库?

在 Kotlin 中设置 startActivity() 时类型不匹配

Material Design 网站的新设计已启动,但我找不到实现选项卡,该选项卡在哪里?

在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递

Android - 水平(从右到左)圆形背景 colored颜色 过渡