android 如何获得Jetpack合成材料3色调的颜色和变化?

pdkcd3nj  于 2023-05-15  发布在  Android
关注(0)|答案(2)|浏览(166)

我在我的Jetpack Composer应用程序中使用动态主题,但所有的颜色都太暗了。看看我的顶部应用程序栏。如何访问colorScheme的较浅变化,例如我的原色?

baubqpgj

baubqpgj1#

您可以用途:

MaterialTheme.colorScheme.primary

以访问当前的主题主颜色。
同时确保您的测试设备/模拟器没有处于暗模式,因为如果是,应用程序会自动切换到暗色调方案。
下面是一个带有Material主题颜色的TopAppBar示例:

@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun testingStuff() {
    TopAppBar(
        title = {
            Text(text = "Doing Stuff",
                color = MaterialTheme.colorScheme.onPrimaryContainer,
                fontStyle = FontStyle.Normal
            )
        },
        colors = TopAppBarDefaults.mediumTopAppBarColors(
            containerColor = MaterialTheme.colorScheme.primaryContainer,
            titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer,
            navigationIconContentColor = MaterialTheme.colorScheme.onPrimaryContainer,
            actionIconContentColor = MaterialTheme.colorScheme.onPrimaryContainer,
            scrolledContainerColor = MaterialTheme.colorScheme.onPrimaryContainer

        ),
        navigationIcon = {
            IconButton({ /*do stuff*/ }) {
                Icon(
                    imageVector= Icons.Default.Menu,
                    contentDescription = "Toggle drawer"
                )
            }
        }
    )
}

您也可以通过编辑Theme.kt文件中的colorSchemes来自定义主题颜色,例如:

private val LightColorScheme= lightColorScheme(
    primary = md_theme_light_primary,
    onPrimary = md_theme_light_onPrimary,
    primaryContainer = md_theme_light_primaryContainer,
    onPrimaryContainer = md_theme_light_onPrimaryContainer,
    secondary = md_theme_light_secondary,
    onSecondary = md_theme_light_onSecondary,
    secondaryContainer = md_theme_light_secondaryContainer,
    onSecondaryContainer = md_theme_light_onSecondaryContainer,
    tertiary = md_theme_light_tertiary,
    onTertiary = md_theme_light_onTertiary,
    tertiaryContainer = md_theme_light_tertiaryContainer,
    onTertiaryContainer = md_theme_light_onTertiaryContainer,
    error = md_theme_light_error,
    onError = md_theme_light_onError,
    errorContainer = md_theme_light_errorContainer,
    onErrorContainer = md_theme_light_onErrorContainer,
    outline = md_theme_light_outline,
    background = md_theme_light_background,
    onBackground = md_theme_light_onBackground,
    surface = md_theme_light_surface,
    onSurface = md_theme_light_onSurface,
    surfaceVariant = md_theme_light_surfaceVariant,
    onSurfaceVariant = md_theme_light_onSurfaceVariant,
    inverseSurface = md_theme_light_inverseSurface,
    inverseOnSurface = md_theme_light_inverseOnSurface,
    inversePrimary = md_theme_light_inversePrimary,
    surfaceTint = md_theme_light_surfaceTint,
    outlineVariant = md_theme_light_outlineVariant,
    scrim = md_theme_light_scrim,
)

上面的颜色只是我的Color.kt文件中的值,你可以根据自己的喜好定制它们。
有关Material 3颜色和主题的详细信息,请参阅:
M3 dynamic colors
M3 custom colors
我希望这能帮上忙,有什么问题尽管问

dphi5xsq

dphi5xsq2#

如果你看一下dynamicLightColorScheme()函数source code,你可以看到那里发生了什么。他们正在创建动态色调调色板,这是内部的,但最终你可以得到你可以在你的应用程序中使用的代码。颜色是这样创建的:

val context = LocalContext.current
val color = Color(context.resources.getColor(colorId, context.theme)

现在你只需要那个colorId。您可以从dynamicTonalPalette()源代码中获取。例如,灯光动态主题的默认原色是android.R.color.system_accent1_600。如果你想要更浅的颜色,你可以试试android.R.color.system_accent1_300

相关问题