避免应用冷启动时候的短暂黑屏的问题
原理
因为在应用启动的时候窗口(window)先创建出来,所以当窗口显示出来的时候我们再通过onCreate方法中执行setContentView方法设置视图的时候窗口已经显示到界面上了,所以导致短暂的黑屏现象。所以为了解决这个问题我们可以预先通过xml来设置window的默认背景。
实现
<style name="splash_style" parent="@android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowContentOverlay">@null</item>
</style>
android:windowBackground设置为你想要的颜色或者图片。
应用该主题
<!-- 程序入口 -->
<activity
android:name="*******"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/splash_style">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>