在Google I/O演示中,谷歌团队表示我们可以禁用默认启动画面。我想这样做,但是我找不到方法。
有人成功实现了吗?
后来编辑:
我误解了视频中讲话者的意思。似乎你只能编辑默认启动画面,而不能禁用它。
有人成功实现了吗?
后来编辑:
我误解了视频中讲话者的意思。似乎你只能编辑默认启动画面,而不能禁用它。
<item name="android:windowIsTranslucent">true</item>
res\values-v31\themes.xml
中覆盖您的启动屏幕主题并设置一个透明的图标。<!-- My custom theme for splash screen activity -->
<style name="Theme.Splash" parent="Theme.Main">
<item name="android:windowBackground">@color/background</item>
<!-- Set a transparent .png as your icon -->
<item name="android:windowSplashScreenAnimatedIcon">@drawable/transparent_image</item>
</style>
没有直接的API可以禁用默认的启动画面,但是如果我们在样式中添加<item name="android:windowIsTranslucent">true</item>
<style name="Theme.RemoveSplashScreenTheme" parent="@style/BaseTheme">
<item name="android:windowIsTranslucent">true</item>
</style>
并将其应用于启动屏幕活动。
<activity
android:name="com.test.SplashScreenActivity"
android:launchMode="singleInstance"
android:theme="@style/Theme.RemoveSplashScreenTheme"
android:noHistory="true" />
这将用透明屏幕替换默认的启动画面。如果应用程序已经有一个启动画面,这个解决方法将消除2个启动画面的问题。
<activity>
元素中的 android:theme
值与 <style>
资源中的主题名称不匹配。我可以让 <style>
方法起作用,但是 android:windowIsTranslucent="true"
在 <activity>
上没有被记录为可用,所以我无法让它起作用。此外,请注意系统启动画面虽然看不见,但仍会在原本应该出现的时间内存在,因此似乎应用程序在启动器图标点击后一段时间内未响应 - 这可能对某些应用程序造成问题。总之,这是一个有趣的技巧! - CommonsWareAndroid 12中引入的默认启动画面禁用并不是一件简单的事情。在我看来,最简单的方法是修改应用程序样式。
在应用程序的values/styles.xml
文件中,确保您有一个“BaseTheme”和一个“AppTheme”。例如:
<resources>
<style name="BaseTheme" parent="Theme.AppCompat">
<!-- [...] -->
</style>
<style name="AppTheme" parent="BaseTheme" />
</resources>
然后,在values-v31/styles.xml
和更高版本(例如v32)中,通过添加以下项扩展“BaseTheme”:
<resources>
<style name="AppTheme" parent="BaseTheme">
<item name="android:windowSplashScreenAnimatedIcon">@android:color/transparent</item>
<item name="android:windowSplashScreenAnimationDuration">0</item>
</style>
</resources>
默认的启动画面只有在应用程序绘制第一帧时才可见。您可以尝试缩短应用程序的启动时间,以防止启动画面的出现。
禁用默认的启动画面是不可能的,但您可以应用补丁来防止其出现,并显示自己的旧定制启动画面。
自定义启动画面,使其透明:
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/transparent</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/transparent</item>
<item name="postSplashScreenTheme">@style/AppTheme</item>
</style>
在这里,您需要在postSplashScreenTheme中设置您的应用程序主题。
将启动画面主题添加到您的启动器活动中:
<activity android:theme="@style/Theme.App.Starting">
在onCreate方法的super.onCreate之前,在您的启动器活动中安装启动画面:
SplashScreen.installSplashScreen(this);
完成!
注意:我在应用程序级别使用AppTheme,并仅在启动器活动中添加透明主题。还在闪屏屏幕主题中使用postSplashScreenTheme属性添加AppTheme。
<style name="SplashTheme" parent="AppMaterialTheme">
<item name="android:windowBackground">@drawable/bg_splash</item>
<item name="android:windowSplashScreenAnimatedIcon">@android:color/transparent</item>
并将其应用于您的 Splash 活动
<activity
android:name=".view.ui.splash.SplashActivity"
android:enabled="true"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">