Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 的 Style 與 Theme:一次搞懂主題與樣式的分層設計

編輯:Android 開發實例

現在幾乎每家公司都會在 Android 上部署自己的客戶端,而使用者對介面美觀度的期待也越來越高。Style 與 Theme 就是 Android 用來統一視覺風格、提升使用者體驗的核心機制。

兩者的分工要分清楚:

  • Theme——作用在「整個窗體」層級,改變 Activity 的整體樣式,例如背景、標題列、動畫。
  • Style——作用在「介面元件」層級,改變指定的控件或 Layout 的外觀。

簡單說,Theme 是房間的裝潢風格,Style 是房間裡每件家具的樣式。

先從系統原始碼學起

Android 系統在 /base/core/res/res/values/ 底下提供了 themes.xml 與 style.xml,裡面定義了大量現成的樣式。強烈建議先從中挑一個接近需求的作為 parent,再繼承修改,而不是從零開始寫。

以下這段是 Theme 中常見的視窗屬性,直接摘自系統的 themes.xml:

<!-- Window attributes -->
<item name="windowBackground">@android:drawable/screen_background_dark</item>
<item name="windowFrame">@null</item>
<item name="windowNoTitle">false</item>
<item name="windowFullscreen">false</item>
<item name="windowIsFloating">false</item>
<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>
<item name="windowTitleStyle">@android:style/WindowTitle</item>
<item name="windowTitleSize">25dip</item>
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Activity</item>

至於控件的 Style,可設定的項目多到看不完——打開 Eclipse 的 Android 屬性編輯器 [Properties] 頁籤掃一遍就知道了。系統內建的 style.xml 也只是在定義每個控件的預設外觀。

有個建議:控件的 Style 不建議大改。使用者對 Android 原生控件已經有肌肉記憶,過度客製反而會造成操作困惑;耐看的樣式才能讓人長期使用。

另外,控件的 Style 在很多情況下都會用到 9.png(Nine-patch,可拉伸點陣圖)。要學好它,最好的教材就是系統內建的檔案,位置在 /base/core/res/res/drawable-hdpi/。

所以,認真研究 Style 與 Theme 的人,強烈建議把 Android 的 base.git 抓下來讀。

本文實作的效果

themes.xml:自訂視窗標題

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <!-- 繼承 Android 內建的 Theme.Light -->
    <style name="Theme" parent="android:Theme.Light">
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowTitleSize">60dip</item>
        <item name="android:windowTitleStyle">@style/WindowTitle</item>
    </style>

    <style name="WindowTitle" parent="android:WindowTitle">
        <item name="android:singleLine">true</item>
        <item name="android:shadowColor">#BB000000</item>
        <item name="android:shadowRadius">2.75</item>
    </style>
</resources>

要讓 Activity 套用這個 theme,兩種方式:在程式碼中呼叫 setTheme(R.style.Theme)(注意必須在 setContentView 之前),或直接在 AndroidManifest 中設定:

styles.xml:控件的樣式

這裡示範讓 Button 看起來像 EditText、EditText 看起來像 Button,用來證明 Style 的替換能力:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="TextView">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">#008</item>
        <item name="android:shadowColor">@android:color/black</item>
        <item name="android:shadowRadius">2.0</item>
    </style>

    <style name="EditText">
        <item name="android:shadowColor">@android:color/black</item>
        <item name="android:shadowRadius">1.0</item>
        <item name="android:background">@android:drawable/btn_default</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
    </style>

    <style name="Button">
        <item name="android:background">@android:drawable/edit_text</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
    </style>
</resources>

版面檔 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <EditText android:id="@+id/EditText01"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:text="類似Button的EditText"></EditText>
    <EditText android:id="@+id/EditText02"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:text="普通的EditText"></EditText>
    <Button android:id="@+id/Button01"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:text="類似EditText的Button"></Button>
</LinearLayout>

一個關鍵細節:Style 名稱即套用方式

上面的 styles.xml 有一個容易忽略的地方:Style 的名稱寫成 TextView、EditText、Button,與控件類名相同。這不是巧合——Android 有個隱性約定,若 style 名稱與控件類名一致,系統會自動把它套用到該類型的控件上,不需要在 XML 逐一指定 style 屬性。

這既是便利也是陷阱:一旦這麼命名,專案中所有同類控件都會受影響,包含第三方函式庫的。若不希望如此,應改用自訂名稱,再透過 style="@style/MyButton" 明確套用。

Theme 與 Style 的繼承關係

兩者底層是同一套機制,差別只在套用範圍。Theme 事實上也可以「 implicitly 」影響到未指定 style 的控件,因為系統預設 theme 裡就包含了各控件的 default style。

理解這層關係後,客製化的策略就清楚了:先選一個接近的系統 Theme 作為 parent,只覆寫你真正想改的屬性;控件層級則用明確命名的 Style 處理特例。這樣樣式碼不會失控,日後要換風格也只改一處。

現代開發的補充

本文示範的是舊式資源檔寫法,概念至今不變,但工具已大幅演進。目前 Android 官方推的是 Material Components:Theme 要繼承 Theme.Material3.* 或 Theme.MaterialComponents.*,色彩透過 colorPrimary、colorSurface 等「主題屬性」(theme attributes)統一管理,而非到處寫死色碼。

更進一步的是 Material You 的動態色彩:Android 12 起可以讓 App 的配色自動跟隨使用者桌布,做法是繼承 Theme.Material3.DynamicColors.*,幾乎不用寫額外程式碼。

若你需要支援深色模式,現在的標準做法是在 res/values/ 與 res/values-night/ 分別放不同色值的 colors.xml,系統會依設定自動切換,不必像早期那樣手動偵測並重建 Activity。

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