android App启动闪白屏解决方案

App启动白屏/黑屏产生原因:
     进入到Activity,但是未加载到布局文件,就先显示来windows窗口的背景。黑屏/白屏就是显示的windows背景(这个就是theme的设置)。

    onCreate---setContentView这个并不是同时进行的,窗体绘制的第一步,系统会在执行这个步骤之前,先绘制窗体,这时候布局资源还没加载,于是就使用默认背景色。

解决方案:
第一种:设置一张背景图片(快速,流畅)

<style name="Theme.AppStartLoad" parent="android:Theme">
 <item name="android:windowBackground">@drawable/tp</item> 
<item name="android:windowNoTitle">true</item> 
</style>

第二种:设置窗口透明(一次性加载成功)

<style name="Theme.AppStartLoadTranslucent" parent="android:Theme">
 <item name="android:windowIsTranslucent">true</item>
 <item name="android:windowNoTitle">true</item> 
</style>

范例,在values/styles.xml中添加文件:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
    </style>

</resources>

猜你喜欢

转载自blog.csdn.net/augfun/article/details/86122852