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

应用主题开发后windowSplashScreenBackground设置失效问题

Android启动页背景随夜间模式切换,无法保持固定品牌色问题

问题详情

此前通过API实现的启动页背景显示正常,完成主题开发与Bug修复后,应用图标仍能显示带有正确背景的圆角方形,但启动页背景变为随夜间模式切换的浅白色(实际值为#fef7ff)或黑色,不符合始终显示品牌色的预期。

应用配置情况:

  • 应用全局主题设置为Theme.Splash,各Activity默认使用Theme.Base.Chalkboard,用户可在设置中切换为Theme.Base.Corporate主题
  • 已尝试过重新生成mipmap图标(SVG/PNG源文件)、禁用旧版图标(目标设备为Android 10及以上)、注释主题代码等操作,问题仍未解决,怀疑android:background属性设置存在影响

提供的代码文件

theme.xml 主题配置

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base Chalkboard Theme. -->
    <style name="Theme.Base.Chalkboard" parent="Theme.Material3.DayNight.NoActionBar">
        <item name="android:background">@color/chalkboard_backgroundColor</item>
        <item name="android:textColor">@color/chalkboard_textColor</item>
        <item name="android:tint">@color/chalkboard_tintColor</item>
        <item name="android:fontFamily">@font/open_sans</item>

        <item name="colorAccent1">@color/chalkboard_colorAcent1</item>
        <item name="colorAccent2">@color/chalkboard_colorAcent2</item>

    </style>

    <!-- Base Corporate Theme. -->
    <style name="Theme.Base.Corporate" parent="Theme.Material3.DayNight.NoActionBar">
        <item name="android:background">@color/corporate_backgroundColor</item>
        <item name="android:textColor">@color/corporate_textColor</item>
        <item name="android:tint">@color/corporate_tintColor</item>
        <item name="android:fontFamily">@font/open_sans</item>

        <item name="android:textColorPrimaryDisableOnly">@color/corporate_textColor</item>
        <item name="android:textColorAlertDialogListItem">@color/corporate_textColor</item>

        <item name="colorAccent1">@color/corporate_colorAcent1</item>
        <item name="colorAccent2">@color/corporate_colorAcent2</item>
    </style>

    <!-- Base splash theme -->
    <style name="Theme.Splash" parent="Theme.Material3.DayNight.NoActionBar">
        <item name="windowSplashScreenBackground">@color/royal_blue</item>
        <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
        <item name="windowSplashScreenAnimationDuration">200</item>
        <item name="postSplashScreenTheme">@style/Theme.Base.Chalkboard</item>
    </style>
</resources>

MainActivity 启动页调用代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Handle the splash screen transition.
    long delay = 578L;
    SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
    splashScreen.setKeepOnScreenCondition(() -> keepSplashOnScreen);
    new Handler(Looper.getMainLooper()).postDelayed(() -> keepSplashOnScreen = false, delay);
}

解决方案

1. 修改启动页主题的父主题,脱离夜间模式控制

启动页需要固定背景色,不能继承随夜间模式切换的Theme.Material3.DayNight.NoActionBar,改用不跟随模式变化的主题父类,同时添加兼容旧逻辑的窗口背景设置:

<style name="Theme.Splash" parent="Theme.Material3.NoActionBar">
    <item name="windowSplashScreenBackground">@color/royal_blue</item>
    <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
    <item name="windowSplashScreenAnimationDuration">200</item>
    <item name="postSplashScreenTheme">@style/Theme.Base.Chalkboard</item>
    <!-- 兼容低版本SplashScreen的窗口背景设置 -->
    <item name="android:windowBackground">@color/royal_blue</item>
</style>

2. 替换全局android:background为窗口背景属性

基础主题中设置的android:background是全局View背景属性,会影响所有控件甚至通过主题继承干扰启动页,应改为仅设置窗口背景的android:windowBackground:
以Theme.Base.Chalkboard为例:

<style name="Theme.Base.Chalkboard" parent="Theme.Material3.DayNight.NoActionBar">
    <!-- 替换原android:background为窗口背景属性 -->
    <item name="android:windowBackground">@color/chalkboard_backgroundColor</item>
    <item name="android:textColor">@color/chalkboard_textColor</item>
    <item name="android:tint">@color/chalkboard_tintColor</item>
    <item name="android:fontFamily">@font/open_sans</item>

    <item name="colorAccent1">@color/chalkboard_colorAcent1</item>
    <item name="colorAccent2">@color/chalkboard_colorAcent2</item>
</style>

Theme.Base.Corporate主题做同样修改,替换android:background为android:windowBackground。

3. 验证品牌色资源是否被夜间模式重写

检查@color/royal_blue的定义,确保在values-night目录下没有被重写为其他颜色,若存在则删除该目录下对应的颜色定义,保证品牌色为固定值。

4. 确认启动页过渡逻辑无干扰

当前MainActivity的启动页代码逻辑正常,无需修改,但需确保在installSplashScreen调用前,没有其他代码提前修改了Activity的主题配置。


内容的提问来源于stack exchange,提问作者Jason 'Slingshot' Miller

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 12:35:00