在开发过程中我们经常会遇到点击启动App的时候有短暂的白屏出现,怎么避免这种情况呢?提供俩种解决思路
1、设置透明的界面来完成
在你的style里面加这俩行代码
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
完整的示例如下
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- 设置透明界面 -->
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
这种方式有一个弊端,当你App做的很大的时候点击启动时总感觉是启动的很慢,其实不是这样的,而是启动的时候透明了,所以感觉是启动的慢
2、把白屏换成一张过度的图片
在你的style里面加这俩行代码
<item name="android:windowBackground">@mipmap/ic_launcher</item>
<item name="android:windowNoTitle">true</item>
完整的示例如下
这种方式会使你的App一直有这个图片背景在显示,不是我们想要的结果,所以我们在Activity中重写一个生命周期中界面加载完成后的回调方法,将背景颜色改为白色
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//修改背景为白色
getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
}
这种方式可以解决App启动白屏的问题,但是启动背景显示的时间太短,不像一般App那样欢迎页显示几秒后跳转,所以建议还是做一个欢迎页比较好,能有效的避免白屏问题还能做广告,但是如果公司产品大大不让做欢迎页那就没办法啦,只能采用这俩种方法来避免这个问题了