Jetpack Compose不使用colorScheme颜色方案。

6
我有一个使用 Jetpack Compose 编写的 Android 应用程序。我正在尝试使用应用程序中定义的 colorScheme 设置图标颜色,但它没有起作用。
以下是我的代码。

Color.kt

import androidx.compose.ui.graphics.Color

val green = Color(0xFF61FF67)

Theme.kt

private val MesColorDark = darkColorScheme(
        primary = green,
        secondary = green,
        tertiary = green,
        surface = green
)

private val MesColorLight = lightColorScheme(
        primary = green,
        secondary = green,
        tertiary = green,
        surface = green
)

@Composable
fun MesTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val mesColorScheme =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        } else {
            if (darkTheme) MesColorDark else MesColorLight
        }

    MaterialTheme(
        colorScheme = mesColorScheme,
        typography = MesTypography,
        content = content
    )
}

colors.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <!-- Status bar -->
    <color name="black30">#4D000000</color>
</resources>

themes.xml

<resources>

    <style name="Platform.Theme.Mes" parent="android:Theme.Material.Light.NoActionBar">
        <item name="android:statusBarColor">@color/black30</item>
    </style>

    <style name="Theme.Mes" parent="Platform.Theme.Mes" />

</resources>

然后我有一个定义为以下内容的图标:
Icon(
imageVector = Icons.Outlined.Phone,
contentDescription = "Open navigation drawer",
tint = MaterialTheme.colorScheme.primary
)

这是输出:

enter image description here

如您所见,这种颜色在颜色方案中并未定义。即使我使用surfacebackground等等……它仍然不会变成绿色。
但是,如果我使用这段代码:
Icon(
imageVector = Icons.Outlined.Phone,
contentDescription = "Open navigation drawer",
tint = Colors.Green
)

它变成了这个样子:

enter image description here

有人可以帮忙解释为什么colorScheme没有起作用吗?
3个回答

11
val mesColorScheme = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    val context = LocalContext.current
    if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
} else {
    if (darkTheme) DarkColorScheme else LightColorScheme
}

请在Theme.kt文件中检查上述代码。由于您正在使用Android S或更高版本,动态颜色将被启用。请将该代码更改为以下内容。
val mesColorScheme = if (darkTheme) DarkColorScheme else LightColorScheme

我正在使用Material3,因此颜色已更改为colorScheme。是的,我的根应用程序位于“MesTheme { }”内。 - Mervin Hemaraju
@MervinHemaraju 请检查更新后的答案。我已经验证过了,它可以工作。 - SURYA S R
哦,是的,这个可以工作!但我的应用程序只适用于Q及以上版本。如果我理解正确,您的答案适用于S及以上版本。那么我该如何解决这个问题呢? - Mervin Hemaraju
只有 S 系列及以上的设备才支持 动态颜色 功能。在旧设备上,该功能也能正常工作。我们将禁用所有设备的动态颜色功能,因此在所有设备上,颜色都将如预期一样。 - SURYA S R
请查看此链接以了解动态颜色的概述:https://developer.android.com/jetpack/compose/designsystems/material3#dynamic_color_schemes - SURYA S R
显示剩余2条评论

0

像这样添加 onPrimary

private val MesColorDark = darkColorScheme(
    primary = green,
    onPrimary = green,
    primaryContainer =green,
    onPrimaryContainer = green
)

尝试过了,还是不起作用... - Mervin Hemaraju

0
@Preview(apiLevel = 30, // The API level should be 30 or below for Jetpack Compose to pick the colors from the color schema.)
@Composable
    fun MyComposable() {
        MyTheme {....}
                       }

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