前幾天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