我需要制作一个带有圆角的容器,其中装有一个圆圈,以便一半在容器外部,一半在容器内部。我用下面的代码实现了一个没有圆角的矩形
Box(modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.padding(top = 20.dp)
.background(ColorScheme.Quaternary.toColor().first)
) {
Box(
modifier = Modifier
.layout { measurable, constraints ->
// Measure the child
val placeable = measurable.measure(constraints)
// Set the width and height of the layout as measured
layout(placeable.width, placeable.height) {
// Place the child such that half of it is outside the top boundary
placeable.place(0, -25.dp.roundToPx()) // Moves the circle up by 25dp
}
}
.size(50.dp)
.clip(CircleShape)
.background(Color.Red)
)
}
当我尝试将容器弄圆时,红色圆圈也会被剪掉
这是我在修饰符上调用clip的代码(注意我也尝试在修饰符change的不同位置调用clip,但效果是相同的)
Box(modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.padding(top = 20.dp)
.clip(RoundedCornerShape(10.dp))
.background(ColorScheme.Quaternary.toColor().first)
) {
Box(
modifier = Modifier
.layout { measurable, constraints ->
// Measure the child
val placeable = measurable.measure(constraints)
// Set the width and height of the layout as measured
layout(placeable.width, placeable.height) {
// Place the child such that half of it is outside the top boundary
placeable.place(0, -25.dp.roundToPx()) // Moves the circle up by 25dp
}
}
.size(50.dp)
.clip(CircleShape)
.background(Color.Red)
)
}
大家有什么想法吗?
在 Jetpack Compose 中,DrawScope 内通过 Modifier.drawWBehind/WithContent/WіthCache 的任何可组合项或绘图都不会被剪裁,除非直接或在后台调用父级
Modifier.clip()
调用Modifier.graphicsLayer{clip = true}
。您可以
Modifier.clip(RoundedCornerShape(10.dp))
从父可组合项中删除并更改背景您还可以 Modifier.drawBehind 在可组合项后面绘制圆角矩形和圆圈,如下所示