如何在 Kotlin 中为可组合函数创建一个组合其他注解的注解?

3

我想创建一个名为@MyComposablePreview的注解,它由以下内容组成:

@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = true,
    name = "Light Mode"
)
@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_YES,
    showBackground = true,
    name = "Dark Mode"
)
@Composable

这样我就可以像这样在可组合的函数中调用它:

@MyComposablePreview
fun CardPreiview() {
    //Code of preview
}

在Jetpack Compose中是否可以实现这个功能?我尝试使用注释类,但是无法做到 :/


1
这是一个必需的功能! - Richard Onslow Roper
2
请参考此处类似的问题链接,并在此处点赞 链接 - Alex Lipov
1个回答

0
你可以使用这个:
@Preview(
  name = "Light",
  group = "Light",
  showBackground = true,
  backgroundColor = 0xffEEEEEE,
  uiMode = UI_MODE_NIGHT_NO,
  fontScale = 1f
)
@Preview(
  name = "Dark",
  group = "Dark",
  showBackground = true,
  backgroundColor = 0xff444444,
  uiMode = UI_MODE_NIGHT_YES,
  fontScale = 1f
)
@Preview(
  name = "Light - Large font",
  group = "Light",
  showBackground = true,
  backgroundColor = 0xffEEEEEE,
  uiMode = UI_MODE_NIGHT_NO,
  fontScale = 2f
)
@Preview(
  name = "Dark - Large font",
  group = "Dark",
  showBackground = true,
  backgroundColor = 0xff444444,
  uiMode = UI_MODE_NIGHT_YES,
  fontScale = 2f
)
annotation class MyComposablePreview

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接