You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose:基于系统设置动态应用Android 15色彩对比度

适配Android 15色彩对比度动态切换方案

步骤1:定义对比度级别枚举

先创建枚举类统一管理对比度级别,简化后续逻辑判断:

enum class ColorContrastLevel {
    LOW,
    MEDIUM,
    HIGH
}

步骤2:获取系统对比度设置

通过Android 15(API 35)新增的无障碍API读取系统对比度配置,同时兼容低版本系统(默认使用低对比度):

@Composable
fun getSystemColorContrastLevel(): ColorContrastLevel {
    val context = LocalContext.current
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
        val accessibilityManager = context.getSystemService(AccessibilityManager::class.java)
        when (accessibilityManager.colorContrast) {
            AccessibilityManager.COLOR_CONTRAST_LOW -> ColorContrastLevel.LOW
            AccessibilityManager.COLOR_CONTRAST_MEDIUM -> ColorContrastLevel.MEDIUM
            AccessibilityManager.COLOR_CONTRAST_HIGH -> ColorContrastLevel.HIGH
            else -> ColorContrastLevel.LOW
        }
    } else {
        ColorContrastLevel.LOW
    }
}

步骤3:修改Theme.kt适配对比度切换

更新MesTheme函数,加入对比度级别参数,结合深浅主题选择对应自定义调色板;同时处理动态颜色的自动适配逻辑:

@Composable
fun MesTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    contrastLevel: ColorContrastLevel = getSystemColorContrastLevel(),
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            // Android 15+ 动态颜色会自动响应系统对比度设置
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }
        else -> {
            // 根据深浅主题+对比度级别匹配自定义调色板
            when (darkTheme) {
                true -> when (contrastLevel) {
                    ColorContrastLevel.LOW -> darkLowContrastScheme
                    ColorContrastLevel.MEDIUM -> darkMediumContrastScheme
                    ColorContrastLevel.HIGH -> darkHighContrastScheme
                }
                false -> when (contrastLevel) {
                    ColorContrastLevel.LOW -> lightLowContrastScheme
                    ColorContrastLevel.MEDIUM -> lightMediumContrastScheme
                    ColorContrastLevel.HIGH -> lightHighContrastScheme
                }
            }
        }
    }

    rememberSystemUiController().setSystemBarsColor(
        color = colorScheme.background
    )

    MaterialTheme(
        colorScheme = colorScheme,
        typography = MesTypography,
        shapes = MesShapes,
        content = content
    )
}

关键注意事项

  • 确保已定义对应自定义调色板变量:darkLowContrastScheme、darkMediumContrastScheme、darkHighContrastScheme、lightLowContrastScheme、lightMediumContrastScheme、lightHighContrastScheme,即你之前为不同对比度和主题准备的调色板。
  • Android 15及以上版本开启动态颜色时,系统提供的dynamicDarkColorScheme/dynamicLightColorScheme会自动适配系统对比度设置,无需额外自定义。
  • 低版本系统默认使用低对比度方案,保证兼容性。

内容的提问来源于stack exchange,提问作者Mervin Hemaraju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 18:47:24