如何解决Splash Screen在API 33以下版本无法显示的问题?
Jetpack Splash Screen 在 API 33 以下版本不显示的解决方案
Jetpack Splash Screen 本身支持 API 21+ 版本,但需要正确配置才能在低版本生效,以下是针对性的修复步骤:
1. 确认依赖版本
确保引入的是稳定版的 Splash Screen 库,避免旧版本的兼容问题:
implementation "androidx.core:core-splashscreen:1.0.1"
2. 修复主题配置
Theme.SplashScreen 在 API 33 以下依赖库的兼容实现,同时动画图标仅支持 API 31+,需分版本配置:
<!-- values/styles.xml(适配 API 21-30) --> <style name="Theme.App.Starting" parent="Theme.SplashScreen"> <item name="windowSplashScreenBackground">@color/white</item> <item name="windowSplashScreenAnimationDuration">1000</item> <item name="postSplashScreenTheme">@style/Theme.JetpackIntroduction</item> <!-- 低版本使用静态图标 --> <item name="windowSplashScreenIconBackground">@color/white</item> <item name="windowSplashScreenIcon">@drawable/splash_screen_logo_static</item> </style> <!-- values-v31/styles.xml(适配 API 31+) --> <style name="Theme.App.Starting" parent="Theme.SplashScreen"> <item name="windowSplashScreenAnimatedIcon">@drawable/splash_screen_logo_animated</item> <item name="windowSplashScreenBackground">@color/white</item> <item name="windowSplashScreenAnimationDuration">1000</item> <item name="postSplashScreenTheme">@style/Theme.JetpackIntroduction</item> </style>
3. 配置 AndroidManifest.xml
将启动 Activity 的主题直接设置为 Theme.App.Starting,而非最终主题:
<activity android:name=".MainActivity" android:exported="true" android:theme="@style/Theme.App.Starting"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
4. 调整 MainActivity 代码顺序
必须先调用 installSplashScreen(),再执行 super.onCreate() 和 setContentView():
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { // 此调用必须放在 super.onCreate 之前 val splashScreen = installSplashScreen() super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 如需自定义停留时间,可设置回调(可选) splashScreen.setKeepOnScreenCondition { // 返回 true 保持显示,false 关闭 false } } }
低版本兼容说明
- API 21-30:仅支持静态图标+背景显示,动画效果无法生效,这是库的固有限制。
- 静态图标需符合尺寸要求:建议使用 48x48dp 的透明背景图标,背景色由
windowSplashScreenIconBackground定义。
内容的提问来源于stack exchange,提问作者Mohammed Arbaaz
相关产品推荐
相关产品推荐

