編輯:Android 開發實例
現在幾乎每家公司都會在 Android 上部署自己的客戶端,而使用者對介面美觀度的期待也越來越高。Style 與 Theme 就是 Android 用來統一視覺風格、提升使用者體驗的核心機制。
兩者的分工要分清楚:
簡單說,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 抓下來讀。

<?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 中設定:

這裡示範讓 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>
<?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>
上面的 styles.xml 有一個容易忽略的地方:Style 的名稱寫成 TextView、EditText、Button,與控件類名相同。這不是巧合——Android 有個隱性約定,若 style 名稱與控件類名一致,系統會自動把它套用到該類型的控件上,不需要在 XML 逐一指定 style 屬性。
這既是便利也是陷阱:一旦這麼命名,專案中所有同類控件都會受影響,包含第三方函式庫的。若不希望如此,應改用自訂名稱,再透過 style="@style/MyButton" 明確套用。
兩者底層是同一套機制,差別只在套用範圍。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。
Android JSON 解析實戰:用 JSONObject 取出你真正需要的欄位
JSON 是 JavaScript Object Notation 的縮寫,作為一種輕量、語言中立的資料交換格式,它比 XML 更精簡、更容易閱讀,因此在 Android
Android MediaPlayer(多媒體播放):音訊與影片控制入門
Android 提供了許多方法來控制播放的音訊/影片檔案與串流,其中主要的一種是透過名為 MediaPlayer 的類別。 MediaPlayer 類別讓 Android
在源碼層面獲取當前調用 API 的應用名稱
做 ROM 定製或者修改系統源碼時,經常需要知道「現在是哪個應用在調用我」。比如攝像頭被打開時,想記錄下來是哪個 App 喚醒的。這個信息在 framework 層是可以
Android 應用本地化:values-local 與多語系字串設定
Android 的本地化機制主要是透過不同資源資料夾來達成。當系統語言改變時,Android 會優先載入符合目前語言環境的資源。 資料夾命名規則 本地化字串通常放在 re