前几天Boss就反应说,机器每次启动程序都会闪一下黑屏,这个客户不接受。没办法,只能想一想怎么解决,最后找到了下面的方法。闪黑屏的缘由主要是咱们启动Activity的时候,须要跑完onCreate和onResume才会显示界面。也就是说须要处理一些数据后,才会显示。按照这种思路,是否是我把初始化的工做尽可能减小就能够避免黑屏?事实是,就算你onCreate啥都不作,仍然会闪一下黑屏,由于初始化解析界面时须要必定时间。下面是解决办法:html
(PS:新建的QQ群,有兴趣能够加入一块儿讨论:Android群:322599434) android
一、自定义Themeapi
//Edited by mythou
//http://www.cnblogs.com/mythou/
//一、设置背景图Theme <style name="Theme.AppStartLoad" parent="android:Theme"> <item name="android:windowBackground">@drawable/ipod_bg</item> <item name="android:windowNoTitle">true</item> </style> //二、设置透明Theme <style name="Theme.AppStartLoadTranslucent" parent="android:Theme"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> </style>
上面我定义了两种Theme,第一种Theme就是设置一张背景图。当程序启动时,首先显示这张背景图,避免出现黑屏。第二种Theme是把样式设置为透明,程序启动后不会黑屏而是整个透明了,等到界面初始化完才一次性显示出来。下面说说两种方式的优缺点:app
二、修改AndroidManifest.xml测试
为了使上面Theme生效,咱们须要设置一些Activity的Theme优化
//Edited by mythou
//http://www.cnblogs.com/mythou/
<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>
三、Theme属性详解spa
//Edited by mythou
//http://www.cnblogs.com/mythou/
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" //平板风格显示
四、Theme和Stylecode
Android里面除了Theme外还有Style,例以下面是Launcher里面配置workspace的一个Stylexml
//Edited by mythou
//http://www.cnblogs.com/mythou/
<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有什么区别?下面列出二者区别:htm
上面就是经过Theme解决程序启动闪黑屏问题,而且讲解了Theme和Style,经过Theme配置,其实还能够作个欢迎页面。不过咱们都但愿程序启动速度越快越好,所以仍是须要多多优化本身的程序。
Edited by mythou
原创博文,转载请标明出处:http://www.cnblogs.com/mythou/p/3196042.html