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

Flutter中修改启动屏图标背景色遇属性未找到问题求助

问题描述

我的style.xml内容如下:

<!-- This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@color/background</item>
</style>

遇到错误:

style attribute 'android:attr/windowSplashScreenIconBackground' not found.

尝试将父主题改为@android:style/Theme.SplashScreen.IconBackground,但父类未被识别,错误依旧。

注意:请勿推荐flutter_native_splash包,该方案无效。我的minSDK=31,targetSDK=34。

当前状态:使用上述style.xml可修改启动屏背景色,但图标背景为白色,需改为与背景一致的颜色。

启动屏效果:
启动屏效果


解决方案

针对Android 12+(minSDK31)的SplashScreen API,按以下步骤修改配置:

  1. 修改主题继承关系并配置SplashScreen属性
    将style.xml中的主题改为继承Theme.SplashScreen,并添加图标背景色配置:

    <style name="NormalTheme" parent="@android:style/Theme.SplashScreen">
        <!-- 启动屏背景色 -->
        <item name="android:windowSplashScreenBackground">@color/background</item>
        <!-- 图标背景色,与主背景色保持一致 -->
        <item name="android:windowSplashScreenIconBackground">@color/background</item>
        <!-- 指定启动完成后切换的主题 -->
        <item name="postSplashScreenTheme">@style/AppTheme</item>
    </style>
    
    <!-- 定义启动后的应用主题 -->
    <style name="AppTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">@color/background</item>
    </style>
    
  2. 验证Manifest配置
    确保AndroidManifest.xml中MainActivity的主题指向修改后的NormalTheme:

    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:theme="@style/NormalTheme">
        <!-- 其他原有配置 -->
    </activity>
    
  3. 添加SplashScreen兼容库依赖
    如果Theme.SplashScreen未被识别,在Module级别的build.gradle中添加依赖:

    dependencies {
        implementation 'androidx.core:core-splashscreen:1.0.1'
    }
    
  4. 确认颜色资源定义
    确保colors.xml中已正确定义@color/background:

    <color name="background">#你的目标背景色值</color>
    

说明

Android 12及以上采用全新的SplashScreen API,旧的Theme.Light.NoTitleBar不支持windowSplashScreenIconBackground这类新属性,必须继承Theme.SplashScreen主题才能配置图标背景色。通过postSplashScreenTheme指定启动后的主题,保证应用整体样式一致性。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 16:30:26