gpt4 book ai didi

android - 如何在 Android Jetpack Compose 中更改布局子项的绘制顺序?

转载 作者:行者123 更新时间:2023-12-04 08:11:35 27 4
gpt4 key购买 nike

默认情况下,在 Jetpack Compose 中,布局子级呈现顺序与代码中子级的顺序相匹配。在以下示例中,船(文本)将被绘制在水面(框)上。

@Composable
fun DrawingOrderExample(submarineMode: Boolean) {
Box(contentAlignment = Alignment.Center) {
Box(
modifier = Modifier
.size(32.dp)
.background(Color.Cyan.copy(alpha = .5f))
)

Text("⛵")
}
}
我可以根据 submarineMode 强制将船拖到水下吗?争论?

最佳答案

您可以使用zIndex()修改子绘图顺序的修饰符:

...

import androidx.compose.ui.zIndex

Box {
Text("Drawn second", Modifier.zIndex(1f))
Text("Drawn first")
}
船舶示例:
@Composable
fun DrawingOrderExample(submarineMode: Boolean) {
Box(contentAlignment = Alignment.Center) {
Box(
modifier = Modifier
.size(32.dp)
.background(Color.Cyan.copy(alpha = .5f))
)

val shipZIndex = if (submarineMode) -1f else 1f

Text(
text = "⛵",
modifier = Modifier.zIndex(shipZIndex) // <- here's the trick
)
}
}
现在 submarineMode参数影响绘图顺序:
enter image description here
enter image description here

关于android - 如何在 Android Jetpack Compose 中更改布局子项的绘制顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65923296/

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