迁移至新SplashScreen API后,如何在Android 12以下实现windowSplashScreenBrandingImage?
解决Android 12以下SplashScreen兼容库无法显示底部栏的问题
你遇到的问题本质是:SplashScreen兼容库并没有为Android 12(API 31)以下版本适配windowSplashScreenBrandingImage属性,官方指南里的"一致显示"仅针对核心启动屏元素(背景、中心图标),品牌图这个属性是API 31才原生支持的,兼容库未向下兼容该配置。
下面是几个可行的解决思路:
思路1:低版本复用旧启动屏方案(最简单)
针对Android 12以下版本,不使用Theme.SplashScreen作为父主题,改用传统的启动屏实现方式,直接通过windowBackground配置包含底部栏的图层:
- 在
values/styles.xml中修改启动主题:
<style name="Theme.App.SplashThemeNew" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash_screen_low_version</item> <item name="postSplashScreenTheme">@style/AppTheme</item> </style>
- 创建
drawable/splash_screen_low_version.xml(图层列表,包含背景、中心图标、底部栏):
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 背景色 --> <item android:drawable="@color/white" /> <!-- 中心图标 --> <item android:gravity="center"> <bitmap android:src="@drawable/splash_icon" /> </item> <!-- 底部栏 --> <item android:gravity="bottom"> <bitmap android:src="@drawable/background_splash_12_bottom_strp" /> </item> </layer-list>
values-v31/styles.xml保持你原来的配置不变,这样高版本用原生SplashScreen的品牌图属性,低版本用传统图层实现,各自显示正确。
思路2:在启动Activity中添加自定义视图
通过SplashScreen的回调,在启动屏显示时插入自定义底部栏视图,适合想统一使用SplashScreen流程的场景:
在启动Activity的onCreate中添加以下代码:
// 初始化SplashScreen val splashScreen = installSplashScreen() // 获取启动屏的根视图容器 val splashScreenView = splashScreen.view // 创建底部栏视图 val brandingView = ImageView(this).apply { setImageResource(R.drawable.background_splash_12_bottom_strp) layoutParams = FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT ).apply { gravity = Gravity.BOTTOM } } // 添加到底部 splashScreenView.addView(brandingView) // 处理退出动画,避免视图残留 splashScreen.setOnExitAnimationListener { viewProvider -> brandingView.animate() .alpha(0f) .setDuration(viewProvider.iconAnimationDuration) .start() // 完成默认退出动画 viewProvider.remove() }
注意:这个方法需要处理视图的布局和动画同步,避免和SplashScreen的默认动画冲突。
思路3:自定义启动过渡布局
在启动Activity中先显示自定义的完整启动布局(包含底部栏),再切换到主界面:
- 创建
layout/splash_custom.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/white"> <ImageView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:layout_gravity="center" android:src="@drawable/splash_icon"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/background_splash_12_bottom_strp"/> </LinearLayout>
- 在启动Activity的
onCreate中:
override fun onCreate(savedInstanceState: Bundle?) { // 先设置自定义布局 setContentView(R.layout.splash_custom) // 延迟切换到主界面(或根据业务逻辑触发) Handler(Looper.getMainLooper()).postDelayed({ startActivity(Intent(this, MainActivity::class.java)) finish() }, 2000) }
这种方法完全自定义启动屏,但需要自己处理过渡动画,避免生硬切换。
内容的提问来源于stack exchange,提问作者Vikas Pandey
相关产品推荐
相关产品推荐

