Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 避免 APP 啟動閃黑屏的解決辦法(Theme 與 Style)

編輯:關於 Android 程式設計

前幾天主管反映,程式每次啟動都會閃一下黑屏,客戶無法接受,於是只能想辦法解決,最後找到了下面的做法。閃黑屏的主因是:啟動 Activity 時,必須跑完 onCreate 與 onResume 才會顯示介面,也就是說得先處理一些資料後畫面才出現。照這個思路,把初始化工作降到最低是否就能避免黑屏?事實上,即使 onCreate 什麼都不做,仍會閃一下黑屏,因為解析並載入介面本身就需要時間。以下為解決辦法:

1、自訂 Theme

設置背景圖Theme
<style name="Theme.AppStartLoad" parent="android:Theme"> 
   <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:第一種設定一張背景圖,程式啟動時先顯示這張圖,避免黑屏;第二種把樣式設為透明,啟動後不會黑屏而是整片透明,待介面初始化完成才一次顯示出來。兩種方式的優缺點如下:

• 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 中加入上述樣式。
• 兩種樣式都能避免黑屏,可實際測試後擇一採用。
• 這只是避免了黑屏,若程式初始化啟動慢,仍會給人啟動緩慢的感受,需自行優化初始化流程。

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)用在單獨的 View,例如 Button、TextView 等。
• 主題(Theme)透過 AndroidManifest.xml 中的 <application> 與 <activity> 套用在整個應用或某個 Activity,對整個應用或某個 Activity 產生全域性影響。
• 若應用同時使用了主題,其下的 View 又使用了樣式,當主題與樣式屬性衝突時,樣式的優先級高於主題。

以上就是透過 Theme 解決程式啟動閃黑屏的問題,並說明了 Theme 與 Style 的差異。透過 Theme 設定,其實還能做出歡迎頁面;不過我們都希望程式啟動越快越好,因此仍應多多優化自己的程式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved