gpt4 book ai didi

android - Jetpack Compose "onSurface"颜色不起作用

转载 作者:行者123 更新时间:2023-12-04 23:49:09 25 4
gpt4 key购买 nike

我正在使用 Jetpack Compose 使用 Material Theming 进行测试,但我不确定为什么我不能制作主题的 onSurface色彩工作。
这里是 Theme.ktonSurface颜色设置为 Color.Red :

private val DarkColorPalette = darkColors(
primary = Purple200,
primaryVariant = Purple700,
secondary = Teal200,
onSurface = Color.Red, // <------- HERE
onPrimary = Color.Blue, // <----- HERE
)

private val LightColorPalette = lightColors(
primary = Purple500,
primaryVariant = Purple700,
secondary = Teal200,
onSurface = Color.Red, // <------- AND HERE
onPrimary = Color.Blue, // <----- HERE
)

@Composable
fun ExploringMaterialTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable() () -> Unit
) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}

MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes,
content = content
)
}
这是 Activity :
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApp()
}
}
}

@Preview
@Composable
fun MyApp() {
ExploringMaterialTheme {
// I also tried
// Surface(color = MaterialTheme.colors.surface) {
Surface {
Text(text = "Hello!!", modifier = Modifier.padding(16.dp))
}
}
}
我期待“你好!!”以红色显示,而是以正常黑色显示。有什么我想念的想法吗? 🤔
result
当我在 Surface 中设置颜色时,它可以正常工作零件。 Surface获得权利 on颜色(本例中为 onPrimary):
        Surface(color = MaterialTheme.colors.primary) {
Text(text = "Hello!!", modifier = Modifier.padding(16.dp))
}
result2

最佳答案

Surface可组合用途:

CompositionLocalProvider(
LocalContentColor provides contentColor){
//
content()
}
contentColor定义为:
fun Colors.contentColorFor(backgroundColor: Color): Color {
return when (backgroundColor) {
primary -> onPrimary
primaryVariant -> onPrimary
secondary -> onSecondary
secondaryVariant -> onSecondary
background -> onBackground
surface -> onSurface
error -> onError
else -> Color.Unspecified
}
}
目前您必须指定 surface 在您的主题中着色:
private val LightColorPalette = lightColors(
primary = Blue500,
surface = Color.Yellow)
在这种情况下, Text使用 onSurface颜色。
如果不指定 surfaceSurface 上色组件使用 background作为 colorBackground 和 onBackground对于 Text .

关于android - Jetpack Compose "onSurface"颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67696294/

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