启动APP时出现白屏问题

来源:互联网 发布:中戏老师尹珊珊淘宝 编辑:程序博客网 时间:2024/05/09 22:40

最近发现APP启动时,会出现短暂的白屏,看了一下一些成熟的APP并没有这个情况.
百度并整理了一下.
这里记录一下:

首先,这是正常现象,因为当Activity生命周期走到OnResume()显示界面时,我们的Activity并没有加载到布局资源(OnCreate()->setContentView(R.layout.activity_launch_app)),所以肯定会有一个短暂的白屏时间.
这时显示的是window背景,显示的是黑屏白屏,取决于你的主题设置.

有两个解决方案:

1.自定义一个style,设置背景图,这时,当还没有加载到布局资源时,就会显示这张图片;(项目中布局文件根布局不要写成透明,否则背景都是启动图)
    <style name="My_Start_Theme" parent="@android:style/你想使用的主题">    <item name="android:windowBackground">@drawable/启动页的图片名</item>    <item name="android:windowNoTitle">true</item>    </style>
打开AndroidManifest.xml  在<application 节点中 替换主题.2.把windowBackground设置为透明,这样用户体验就是,我已经点击了这个APP,因为此时北极你个是透明的,给人一种启动延迟的感觉(去让用户怀疑手机卡顿),代码跟上一种同理.
    <style name="My_Start_Theme" parent="@android:style/Theme.NoTitleBar">        <item name="android:windowIsTranslucent">true</item>        <item name="android:windowNoTitle">true</item>    </style>
下面是自定义主题Theme的一些属性,有兴趣的同学可以参考一下:
•android:theme="@android:style/Theme.Dialog"   将一个Activity显示为能话框模式  •android:theme="@android:style/Theme.NoTitleBar"  不显示应用程序标题栏  •android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  不显示应用程序标题栏,并全屏  •android:theme="Theme.Light"  背景为白色  •android:theme="Theme.Light.NoTitleBar"  白色背景并无标题栏   •android:theme="Theme.Light.NoTitleBar.Fullscreen"  白色背景,无标题栏,全屏  •android:theme="Theme.Black"  背景黑色  •android:theme="Theme.Black.NoTitleBar"  黑色背景并无标题栏  •android:theme="Theme.Black.NoTitleBar.Fullscreen"    黑色背景,无标题栏,全屏  •android:theme="Theme.Wallpaper"  用系统桌面为应用程序背景  •android:theme="Theme.Wallpaper.NoTitleBar"  用系统桌面为应用程序背景,且无标题栏  •android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"  用系统桌面为应用程序背景,无标题栏,全屏  •android:theme="Translucent"  半透明  •android:theme="Theme.Translucent.NoTitleBar" 半透明、无标题栏  •android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明、无标题栏、全屏  •android:theme="Theme.Panel"  •android:theme="Theme.Light.Panel"  
0 0