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,按以下步骤修改配置:
修改主题继承关系并配置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>验证Manifest配置
确保AndroidManifest.xml中MainActivity的主题指向修改后的NormalTheme:<activity android:name=".MainActivity" android:exported="true" android:theme="@style/NormalTheme"> <!-- 其他原有配置 --> </activity>添加SplashScreen兼容库依赖
如果Theme.SplashScreen未被识别,在Module级别的build.gradle中添加依赖:dependencies { implementation 'androidx.core:core-splashscreen:1.0.1' }确认颜色资源定义
确保colors.xml中已正确定义@color/background:<color name="background">#你的目标背景色值</color>
说明
Android 12及以上采用全新的SplashScreen API,旧的Theme.Light.NoTitleBar不支持windowSplashScreenIconBackground这类新属性,必须继承Theme.SplashScreen主题才能配置图标背景色。通过postSplashScreenTheme指定启动后的主题,保证应用整体样式一致性。
内容的提问来源于stack exchange,提问作者user7524206

