Android 避免APP启动闪黑屏的解决办法(Theme和Style)

来源:互联网 发布:管家婆软件使用感想 编辑:程序博客网 时间:2024/05/29 15:01

闪黑屏的原因主要是我们启动Activity的时候,需要跑完onCreate和onResume才会显示界面。也就是说需要处理一些数据后,才会显示。按照这种思路,是不是我把初始化的工作尽量减少就可以避免黑屏?事实是,就算你onCreate啥都不做,仍然会闪一下黑屏,因为初始化解析界面时需要一定时间。下面是解决办法:
1、自定义Theme

设置背景图Theme<style name="Theme.AppStartLoad" parent="android:Theme">  a    <item name="android:windowBackground">@drawable/ipod_bg</item>      <item name="android:windowNoTitle">true</item>  </style>//2、设置透明Theme<style name="Theme.AppStartLoadTranslucent" parent="android:Theme">      <item name="android:windowIsTranslucent">true</item>     <item name="android:windowNoTitle">true</item>  </style>

上面我定义了两种Theme,第一种Theme就是设置一张背景图。当程序启动时,首先显示这张背景图,避免出现黑屏。第二种Theme是把样式设置为透明,程序启动后不会黑屏而是整个透明了,等到界面初始化完才一次性显示出来。下面说说两种方式的优缺点:
•Theme1 程序启动快,界面先显示背景图,然后再刷新其他界面控件。给人刷新不同步感觉。
•Theme2 给人程序启动慢感觉,界面一次性刷出来,刷新同步。
 
2、修改AndroidManifest.xml
为了使上面Theme生效,我们需要设置一些Activity的Theme
<application    android:allowBackup="true"    android:icon="@drawable/ipod_icon"    android:label="@string/app_name"    android:launchMode="singleTask"><!-- iPod主界面 --><activity    android:name="com.apical.apicalipod.IPodMainActivity"  <!-- 使用上面定义的样式 mythou-->    android:theme="@style/Theme.AppStartLoad"    android:label="@string/app_name" >    <intent-filter>    <action android:name="android.intent.action.MAIN" />    <category android:name="android.intent.category.LAUNCHER" />    </intent-filter></activity>//......</application>

•可以在Activity里面增加上面自定义的样式。另外在Application里面增加也是可以的,而且是全局效果。
•自定义Theme放在 /res/values/styles.xml 里面。如果没有这个文件,自己添加一个即可。
•如果存在多个Activity切换,中间也可能会存在短暂黑屏问题。原因也是Activity启动的时候需要初始化加载数据,如果想避免这种情况,可以在你切换的Activity里面增加上面的样式。
•上面两种样式都可以避免黑屏。可以实际测试一下你的程序选择一种效果。
•这个只是把黑屏避免了,但是如果你程序初始化启动慢,还是会给人程序启动慢的感觉。需要自行优化程序初始化过程。
 
3、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="Theme.Translucent" //透明背景android:theme="Theme.Translucent.NoTitleBar" //透明背景并无标题android:theme="Theme.Translucent.NoTitleBar.Fullscreen" //透明背景并无标题,全屏android:theme="Theme.Panel " //面板风格显示android:theme="Theme.Light.Panel" //平板风格显示


4、Theme和Style
Android里面除了Theme外还有Style,例如下面是Launcher里面配置workspace的一个Style

 <style name="WorkspaceIcon">        <item name="android:layout_width">match_parent</item>        <item name="android:layout_height">match_parent</item>        <item name="android:layout_gravity">center</item>        <item name="android:gravity">center_horizontal</item>        <item name="android:singleLine">true</item>        <item name="android:ellipsize">marquee</item>        <item name="android:textSize">12sp</item>        <item name="android:textColor">#FFF</item>        <item name="android:shadowRadius">2.0</item>        <item name="android:shadowColor">#B0000000</item>    </style>

Style可以理解为一组属性集合,方便不同的View设置使用,我们在View里面使用Style的时候,跟使用Theme是一样的应用方法。那么Style和Theme有什么区别?

0 0