在谷歌I/O演示中,谷歌团队表示我们可以禁用默认的启动屏幕。我想那样做,但我找不到方法。
有人能做到这一点吗?
后期编辑:
我误解了演讲者在视频中说的话。似乎你只能编辑默认的启动屏幕,而不能禁用它
您可以添加此行:
<item name="android:windowIsTranslucent">true</item>
在关闭style标记之前在style.xml文件中。它会让你默认的android splash透明!
在Android 12上,不可能选择退出启动屏幕。只能自定义:图标、窗口背景、退出动画。
默认情况下,启动屏幕从用户触摸开始显示,直到绘制应用程序的第一帧为止,因此为了最大限度地缩短显示启动屏幕的时间,您可以尝试缩短应用程序的启动时间。
你也可以实现自己的退出动画,这样从启动屏幕到应用程序的过渡就更无缝了。
如果您以前使用过专门的SplashScreen活动并希望保留该活动,那么以下是官方文档中的解决方法。
不幸的是,您无法通过Android 12设备提供的API直接禁用默认启动屏幕。
如果你的应用程序有一个自定义的启动屏幕&如果你不想迁移到这种新方法,你可以尝试下面的破解方法。
基本上,你要做的是在resvalues-v31themes.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启动屏幕问题,则此解决方法将消除该问题。
禁用安卓12中引入的默认启动屏幕并非易事。在我看来,最简单的方法是修改应用程序的样式。
在你的应用程序的values/styles.xml
文件中,确保你有一个";基本主题";和一个";AppTheme";。例如:
<resources>
<style name="BaseTheme" parent="Theme.AppCompat">
<!-- [...] -->
</style>
<style name="AppTheme" parent="BaseTheme" />
</resources>
然后,在values-v31/styles.xml
和更新的版本(例如,v32(中;基本主题";通过添加以下项目:
<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方法的创建:
SplashScreen.installSplashScreen(this);
完成!
注意:我在应用程序级别使用AppTheme,并且仅在启动器活动中添加透明主题。还使用postSplashScreenTheme属性在启动屏幕主题中添加AppTheme。
添加"android:windowSplashScreenAnimatedIcon";在您的飞溅主题中
<style name="SplashTheme" parent="AppMaterialTheme">
<item name="android:windowBackground">@drawable/bg_splash</item>
<item name="android:windowSplashScreenAnimatedIcon">@android:color/transparent</item>
并将其应用于您的飞溅活动
<activity
android:name=".view.ui.splash.SplashActivity"
android:enabled="true"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">