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

Android 主題與樣式(上):系統 Theme 的來源、選擇邏輯與屬性分類

編輯:關於 Android 程式設計

幾乎每家公司或每個行動團隊,最終都想做出一套屬於自己的 UI 框架,把自家設計與特性融進去。這必然意味著要改動 Android 的 UI,而要做好這件事,理解系統的主題與樣式是最基礎功課。

一、系統主題定義在哪裡

在 frameworks/base/core/res/res/values 目錄下,有四個關鍵檔案:

themes.xml
themes_device_defaults.xml
styles.xml
styles_device_defaults.xml

它們分別定義了系統的各種 Theme 與 Style。

二、系統如何挑選預設主題

主要關注 themes.xml 與 themes_device_defaults.xml。前者定義了 Android 低版本的 theme 與 Holo theme;後者定義 DeviceDefault 主題,它繼承自 Holo,本質上是「在 Holo 之上做定製」的機制,留給各家廠商發揮。

選擇邏輯寫在 Resources.java 中:

public static int selectDefaultTheme(int curTheme, int targetSdkVersion) {
    return selectSystemTheme(curTheme, targetSdkVersion,
            com.android.internal.R.style.Theme,
            com.android.internal.R.style.Theme_Holo,
            com.android.internal.R.style.Theme_DeviceDefault);
}

public static int selectSystemTheme(int curTheme, int targetSdkVersion,
        int orig, int holo, int deviceDefault) {
    if (curTheme != 0) {
        return curTheme;                 // 已明確指定則尊重它
    }
    if (targetSdkVersion < Build.VERSION_CODES.HONEYCOMB) {         // < 11
        return orig;
    }
    if (targetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {// < 14
        return holo;
    }
    return deviceDefault;
}

結論很清晰:targetSdkVersion 小於 11 使用舊版主題;11 到 13 之間使用 Holo;14 及以上使用 DeviceDefault。這也是為什麼「把 targetSdk 調高」會讓 App 外觀突然改頭換面的原因。

三、主題列表與派生關係

系統預設的三大主題是 Theme、Theme.Holo、Theme.DeviceDefault,但在此基礎上系統還定義了大量派生主題,最典型的就是各自對應的 Light 版本(Theme.Holo.Light、Theme.DeviceDefault.Light 等),以及帶 ActionBar、NoTitleBar、Wallpaper 等變體。理解這層繼承關係後,就能依實際需求在自己的應用中挑選合適的父主題。

四、一個完整主題包含哪些內容

以 Theme 為例,一個完整的主題大致涵蓋以下幾類屬性:

1. 顏色

<item name="colorForeground">@android:color/bright_foreground_dark</item>
<item name="colorBackground">@android:color/background_dark</item>
<item name="colorPressedHighlight">@color/legacy_pressed_highlight</item>
<item name="colorFocusedHighlight">@color/legacy_selected_highlight</item>
<item name="colorActivatedHighlight">@color/legacy_selected_highlight</item>

2. 字體與文字樣式

這一類最龐大,包含 textAppearance 系列、textColorPrimary / Secondary / Tertiary 及其 Inverse、NoDisable、DisableOnly 變體,還有 textColorHint、textColorHighlight、textColorLink、editTextColor,以及大小對應的 textAppearanceLarge / Medium / Small。

3. 按鈕

<item name="buttonStyle">@android:style/Widget.Button</item>
<item name="buttonStyleSmall">@android:style/Widget.Button.Small</item>
<item name="buttonStyleInset">@android:style/Widget.Button.Inset</item>
<item name="buttonStyleToggle">@android:style/Widget.Button.Toggle</item>
<item name="selectableItemBackground">@android:drawable/item_background</item>
<item name="borderlessButtonStyle">?android:attr/buttonStyle</item>

4. 清單

包含 listPreferredItemHeight(預設 64dip)及其 Small / Large 變體、textAppearanceListItem,以及 listPreferredItemPaddingStart / End / Left / Right 等內距設定。

5. 視窗

<item name="windowBackground">@android:drawable/screen_background_selector_dark</item>
<item name="windowNoTitle">false</item>
<item name="windowFullscreen">false</item>
<item name="windowTitleSize">25dip</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">false</item>
<item name="windowTranslucentStatus">false</item>
<item name="windowSoftInputMode">stateUnspecified|adjustUnspecified</item>

6. 對話框相關

dialogTheme、alertDialogTheme、alertDialogStyle,以及 dialogTitleDecorLayout、dialogCustomTitleDecorLayout 等布局指向。

7. 面板

panelBackground、panelFullBackground、panelColorForeground、panelTextAppearance、panelMenuListWidth 等。

8. 捲軸

scrollbarSize(預設 10dip)、scrollbarThumbHorizontal / Vertical、scrollbarFadeDuration(250)、scrollbarDefaultDelayBeforeFade(300)等。

9. 文字選取

textSelectHandleLeft / Right、textSelectHandleWindowStyle,以及 textEditPasteWindowLayout、textEditNoPasteWindowLayout、textSuggestionsWindowStyle、textCursorDrawable 等與選取手柄、貼上選單相關的資源指向。

篇幅關係,剩下的屬性留待下篇繼續列舉,並進一步深入分析具體的 style 該怎麼覆寫與繼承。

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