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

Jetpack Compose Material3主题颜色覆盖在API31+失效求助

问题:Jetpack Compose Material3 自定义颜色在API31+设备不生效及依赖更新冲突

我是Jetpack Compose新手,使用Material3框架自定义了应用的background和surface颜色,但该设置仅在API30及以下的虚拟设备中生效,在API31的物理设备及其他API30以上的虚拟设备中均无作用。

主题代码

private val DarkColorScheme = darkColorScheme(
    primary = Purple80,
    secondary = PurpleGrey80,
    tertiary = Pink80,
    surface = Color(0XFF0C134F),
    background = Color(0XFF0C134F),
    onSurface = Color(0xffffffff)
)

private val LightColorScheme = lightColorScheme(
    primary = Purple40,
    secondary = PurpleGrey40,
    tertiary = Pink40,
    surface = Color(0XFFB6EAFA),
    background = Color(0XFFB6EAFA),

    /* Other default colors to override
    background = Color(0xFFFFFBFE),
    surface = Color(0xFFFFFBFE),
    onPrimary = Color.White,
    onSecondary = Color.White,
    onTertiary = Color.White,
    onBackground = Color(0xFF1C1B1F),
    onSurface = Color(0xFF1C1B1F),
    */
)

@Composable
fun JetweatherTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor  -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            window.statusBarColor = colorScheme.primary.toArgb()
            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
        }
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}

依赖配置

def room_version = "2.5.1"

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.5.1'
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
//HILT
implementation "com.google.dagger:hilt-android:2.44.2"
kapt "com.google.dagger:hilt-compiler:2.45"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation "androidx.hilt:hilt-navigation-compose:1.1.0-alpha01"

//material icons - use with caution!
// implementation "androidx.compose.material:material-icons-extended:$compose_version"
// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.6.4"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'

// OkHttp
implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.2")

// JSON Converter
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

//Room
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

// To use Kotlin annotation processing tool (kapt) MUST HAVE!
kapt("androidx.room:room-compiler:$room_version")
implementation "androidx.room:room-ktx:$room_version"

// Coil
implementation("io.coil-kt:coil-compose:1.4.0")

依赖更新问题

尝试更新以下依赖至最新版本时会出现重复类错误:

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.5.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"

解决方案

1. 解决API31+设备自定义颜色不生效问题

原因是API31(Android 12)及以上默认开启了动态颜色(dynamicColor=true),此时会优先使用系统基于壁纸生成的动态配色方案,完全覆盖了你自定义的DarkColorScheme和LightColorScheme。

有三种解决方式:

  • 方式一:关闭动态颜色
    在主题函数中默认关闭动态颜色,或者在调用主题时传入dynamicColor=false:

    @Composable
    fun JetweatherTheme(
        darkTheme: Boolean = isSystemInDarkTheme(),
        dynamicColor: Boolean = false, // 修改为false
        content: @Composable () -> Unit
    ) {
        // 原有逻辑不变
    }
    
  • 方式二:在动态颜色基础上覆盖指定颜色
    如果想保留动态颜色特性,但替换background和surface,可以在生成动态配色后调用copy()方法修改:

    val colorScheme = when {
        dynamicColor -> {
            val context = LocalContext.current
            val baseScheme = if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
            // 覆盖自定义的颜色
            baseScheme.copy(
                surface = if (darkTheme) Color(0XFF0C134F) else Color(0XFFB6EAFA),
                background = if (darkTheme) Color(0XFF0C134F) else Color(0XFFB6EAFA)
            )
        }
        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }
    
  • 方式三:确保界面正确使用主题颜色
    检查你的Composable是否直接引用了MaterialTheme.colorScheme中的颜色,而不是硬编码值。例如:

    // 正确用法
    Box(
        modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background)
    ) {
        // 界面内容
    }
    

2. 解决依赖更新重复类错误

原因是你手动指定的core-ktx、lifecycle、activity版本与compose-bom管理的兼容版本冲突,导致类重复。

解决方案:

  • 移除手动版本,让Compose BOM自动管理
    删掉这些依赖的具体版本号,依赖会自动使用BOM指定的兼容版本:
    // 修改后
    implementation 'androidx.core:core-ktx'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx'
    implementation 'androidx.activity:activity-compose'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx'
    
  • 若需手动指定版本,确保与BOM兼容
    如果必须使用特定版本,需要查看当前compose-bom:2022.10.00对应的依赖版本表,选择完全匹配的版本,避免版本冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 15:07:04