简体   繁体   English

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义颜色?

[英]How can I define a color in Color.kt with simple way when I use Jetpack Compose?

I can use Code A to define a color in Color.kt file which is generated by Android Studio wizard automatically, it's OK.我可以使用代码A在Android Studio向导自动生成的Color.kt文件中定义颜色,没关系。

I hope to define a color based MaterialTheme.colors , but Code B return the following error.我希望定义一个基于颜色的MaterialTheme.colors ,但是代码 B 返回以下错误。

@Composable invocations can only happen from the context of a @Composable function @Composable 调用只能在 @Composable function 的上下文中发生

At present, I have to use Code C, it's not very good, is there a better way?目前,我必须使用代码 C,不太好,有没有更好的方法?

Code A代码 A

val IconColor = Color(0xFF2E7D32)

Code B代码 B

val IconColor = MaterialTheme.colors.onSurface.copy(alpha = 0.76f)

Code C代码 C

@Composable
fun IconColor(): Color {
    return MaterialTheme.colors.onSurface.copy(alpha = 0.76f)
}

You are getting that because MaterialTheme.colors the getter of this field is using @compsable scope, So I have found 2 ways you can declare your variable in Colors.kt in the way you want.你得到这个是因为MaterialTheme.colors这个字段的吸气剂正在使用@compsable scope,所以我找到了两种方法,你可以用Colors.kt的方式声明你的变量。

1: 1:

 val IconColor1: @Composable () -> Color = { MaterialTheme.colors.onSurface.copy(alpha = 0.76f) }

2: 2:

 val IconColor2: Color @Composable get() = MaterialTheme.colors.onSurface.copy(alpha = 0.76f)

And I hope this was helpful我希望这会有所帮助

Studio has the source for the material colours. Studio 有材料颜色的来源。 Just Ctrl + Click on the name of the class/color and you're there.只需Ctrl + Click类/颜色的名称即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何直接在 Jetpack Compose 中使用 Color 资源? - How do I use Color resource directly in Jetpack Compose? 当我使用 Jetpack Compose 时,在资源文件中声明颜色并直接为 val 分配颜色的更好方法是什么? - Which is better way to declare color between in resource file and assign a color to val directly when I use Jetpack Compose? 使用 Jetpack Compose 时,如何创建基于 TextStyle 的 MaterialTheme.typography.body2 和 Color.Red 的实例? - How can I create a instance of TextStyle based MaterialTheme.typography.body2 and Color.Red when I use Jetpack Compose? 当我以默认设置使用 Jetpack Compose 时,Text("Hello") 如何显示字体颜色? - How does Text("Hello") display font color when I use Jetpack Compose with default settings? Color.kt 中定义的自定义颜色未出现在 MaterialTheme.colors 中 - Custom Color defined in Color.kt is not appearing in MaterialTheme.colors 我们如何在 jetpack compose 中使用颜色网格视图? - How we can use color grid view in jetpack compose? 我可以在 Jetpack Compose 的 MaterialTheme 中使用名称自定义颜色吗? - Can I customize a color with a name in MaterialTheme in Jetpack Compose? 如何使用jetpack compose为复选框设置边框颜色 - How do I set border color for the checkbox using jetpack compose 如何在 Jetpack compose 中更改边框的颜色,而与它的宽度和形状无关? - How can I change border's color independent of it's width and shape in Jetpack compose? 如何将 CameraView 与 Jetpack Compose 结合使用? - How can I use a CameraView with Jetpack Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM