我正在向一个现有的应用程序中添加一个Android 12+启动屏幕。我想保留Android < 12的现有启动屏幕,所以我在“values v31”文件夹中定义了“新”启动屏幕样式。这是res/values-v31/styles.xml
的内容
<resources>
<!-- Splash Screen Theme (Android 12+)
Ref: https://developer.android.com/develop/ui/views/launch/splash-screen/migrate
-->
<style name="AppTheme.Splash">
<item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_foreground</item>
<item name="android:windowSplashScreenBackground">@color/primary</item>
</style>
</resources>
当在模拟器中测试时(使用Pixel4a设备),这是很好的。然而,当在真实设备(Poco X4 Pro 5G)中进行测试时,我发现当设备在夜间模式下配置时,背景色会被忽略。将styles.xml文件复制到“Val值-夜晚-v31”文件夹中可以解决问题,但我认为这不应该是必要的。
我是不是遗漏了什么?
发布于 2022-12-02 12:48:52
看起来您在一个值-v31文件夹中定义了启动屏幕主题,这意味着它只用于运行Android 12或更高版本的设备。如果您想在运行Android 11或更低版本的设备上使用相同的启动屏幕主题,则需要为这些版本创建一个单独的文件夹。
此外,您似乎正在使用android:windowSplashScreenBackground属性来指定启动屏幕的背景色。只有在运行Android 12或更高版本的设备上才支持此属性,而运行Android 11或更低版本的设备不支持此属性。
要解决这个问题,您可以为夜间模式样式创建一个单独的值-夜-v31文件夹,并使用android:windowBackground属性指定启动屏幕背景色。在所有运行Android 11或更高版本的设备上都支持此属性,并且不受设备的夜间模式设置的影响。
下面是如何在styles.xml文件中定义启动屏幕主题的示例:
<resources>
<!-- Splash Screen Theme (Android 12+)
Ref: https://developer.android.com/develop/ui/views/launch/splash-screen/migrate
-->
<style name="AppTheme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_foreground</item>
<item name="android:windowBackground">@color/primary</item>
</style>
然后,您可以使用一个styles.xml文件创建一个单独的值-夜-v31文件夹,该文件指定启动屏幕主题的夜间模式版本:
<resources>
<style name="AppTheme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowSplashScreenAnimatedIcon">@drawable/ic_foreground</item>
<item name="android:windowBackground">@color/primary_dark</item>
</style>
这将确保在所有运行Android 11或更高版本的设备上正确应用启动屏幕主题,而不管设备的夜间模式设置如何。
https://stackoverflow.com/questions/74655886
复制相似问题