Splash Screen夜间模式下背景颜色不更新问题求助
Hey there! Let's tackle why your splash screen is stuck on white even when dark mode is enabled. I've run into this exact issue before, so here's what to check and fix:
1. Make Sure Your Color Resource Has Dark Mode Support
The most common culprit here is that your @color/mainBG doesn't have a corresponding dark variant.
- In
res/values/colors.xml(light mode):<color name="mainBG">#FFFFFF</color> - You must create a
res/values-night/colors.xmlfile for dark mode, with the dark version of the color:<color name="mainBG">#1A1A1A</color> <!-- Or your preferred dark background color -->
If this file is missing or the color isn't defined here, the system will fall back to the light mode color, hence the white background in dark mode.
2. Verify Your Splash Screen Theme Inheritance
Your SplashScreen theme's parent (Theme.StookPTU) might not be set up to support day/night switching. Let's adjust that:
Update your theme in res/themes.xml and res/themes.xml(night) to inherit from a day-night compatible parent, like Theme.MaterialComponents.DayNight.NoActionBar (or whatever fits your app's base theme):
<!-- res/themes.xml (light mode) --> <style name="SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <item name="android:windowBackground">@drawable/splash_screenbg</item> <!-- Add any other splash screen-specific styles here --> </style> <!-- res/themes.xml(night) (dark mode) --> <style name="SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <item name="android:windowBackground">@drawable/splash_screenbg</item> <!-- Keep this consistent since the drawable uses a theme-aware color --> </style>
This ensures the theme respects the system's dark mode setting and loads the correct color resources.
3. Double-Check Your Manifest & App Dark Mode State
- Confirm your
AndroidManifest.xmlis correctly using theSplashScreentheme for your launch activity (your existing code looks good here, but it never hurts to double-check!). - Make sure dark mode is actually enabled: Either toggle it in your device's system settings, or if your app has an in-app dark mode toggle, ensure it's triggering the theme change properly.
- Sometimes cached resources can cause issues—try clearing your app's cache and restarting it to rule out stale resources.
4. Bonus: Check for Overrides
If you're targeting Android 10 (API 29+) or higher, ensure your theme doesn't have android:forceDarkAllowed="false" set, which would prevent dark mode from applying. You can explicitly set it to true if needed:
<item name="android:forceDarkAllowed">true</item>
Once you've made these changes, your splash screen should correctly switch between light and dark backgrounds based on the system (or app) mode.
内容的提问来源于stack exchange,提问作者Daksh

