【问题标题】:How to create BadgeBox with latest compose version? [duplicate]如何使用最新的 compose 版本创建 BadgeBox? [复制]
【发布时间】:2023-02-19 01:34:50
【问题描述】:
对于 Jetpack Compose 版本“1.0.1”,我使用了这样的 Badge Box
BadgedBox(
badgeContent = {
Text(text = "5")
},
backgroundColor = Primary,
modifier = Modifier
) {
Icon(
imageVector = Icons.Outlined.ShoppingCart,
contentDescription = "shopping cart",
tint = Color.Black
)
}
但是对于 Compose 版本“1.0.5”,此代码不起作用
新的方法是什么?
【问题讨论】:
标签:
android
android-studio
kotlin
【解决方案1】:
与撰写M2(从1.1.x 开始)和M3您可以使用:
BadgedBox(
badge = { Badge { Text("5") } },
modifier = Modifier.background(Red)) {
Icon(
Icons.Filled.Favorite,
contentDescription = "Favorite"
)
}
旧答案:
相反,您可以使用 compose M2 1.0.x:
BadgeBox(
badgeContent = { Text("5") },
backgroundColor = Red,) {
Icon(
Icons.Filled.Favorite,
contentDescription = "Favorite"
)
}
在 1.1.x 中,BadgeBox 被重命名为 BadgedBox 并删除了一些参数 (check this commit)。
【解决方案2】:
使用 badge BadgedBox 和 Badge 的属性可组合。
例子,
BadgedBox(
badge = {
Badge {
Text(text = "5")
}
},
// Other attributes remain same
) {
// As it is
}