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

prdp8dxp  于 2023-03-28  发布在  Android
关注(0)|答案(1)|浏览(120)

我想创建一个名为***@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中做到这一点?我尝试使用带注解的类,但我不能:/

iqjalb3h

iqjalb3h1#

您可以使用以下命令:

@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

相关问题