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

自訂 Android App 外觀:Style 與 Theme 的差異與實作

編輯:關於 Android 程式設計

Android 允許開發者自訂主題與樣式。當同一組屬性(寬高、字級、字色、背景……)在多個地方重複出現時,就該把它抽出來變成 Style,讓版面檔保持乾淨,日後改動也只需改一處。

Style:把共用屬性打包

在 res/values/ 底下新增一個 styles.xml,以 <resources> 為根節點,內部放 <item>。item 的 name 就是屬性名,內容就是屬性值:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#987456</item>
        <item name="android:textSize">24sp</item>
    </style>
</resources>

這裡的 parent 屬性很關鍵:它宣告這個 Style 繼承自哪一個既有 Style。繼承而來的所有屬性都會被帶進來,而且你可以針對其中任何一項覆寫成自己想要的值——不必從零開始。

寫好之後,在版面檔裡直接引用即可。以下用一個 EditText 示範:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="@style/MyText"
        android:text="測試一下下" />
</LinearLayout>

Theme:套用範圍更大的 Style

Theme 與 Style 本質相同,差別只在套用範圍:Style 作用在單一 View 上,Theme 則作用在整個 Application 或 Activity 上。所以凡是涉及視窗層級的設定(標題列、視窗邊框、背景),都必須用 Theme。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#987456</item>
        <item name="android:textSize">24sp</item>
    </style>

    <style parent="@android:style/Theme" name="CustomTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFrame">@drawable/icon</item>
        <item name="android:windowBackground">?android:windowFrame</item>
    </style>
</resources>

這個 CustomTheme 繼承自系統預設的 Theme,並覆寫了三個屬性。第三個屬性的寫法要特別注意:?android:windowFrame 前面那個問號代表「引用同一主題中某個 item 的值」,也就是取用上面定義的 icon 圖片。用 @ 是直接指向資源,用 ? 是取值於目前主題中的屬性,兩者意義不同,寫錯就會找不到資源。

在 Manifest 中套用 Theme

於 AndroidManifest.xml 的 <application> 加上 android:theme,整個 App 的外觀就換了:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/CustomTheme">

    <activity
        android:name=".TestStyle"
        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 長得不一樣,把 android:theme 移到該 <activity> 標籤底下即可覆寫全域設定。

另一條路:在程式碼中設定

Theme 也可以在 Java 裡指定,但有一個順序上的硬性要求——必須在 setContentView() 之前呼叫 setTheme(),否則畫面已經配置完成,主題不會生效。

package com.test.shang;

import android.app.Activity;
import android.os.Bundle;

public class TestStyle extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.CustomTheme);   // 一定要在 setContentView 之前
        setContentView(R.layout.test_style);
    }
}

實務建議

優先在 Manifest 裡宣告 Theme,讓全域外觀一致;只有特殊頁面才用程式碼覆寫。至於 Style,則盡量把重複屬性抽成命名樣式,特別是清單項與按鈕這類大量重複使用的元件,日後改設計稿時會省下大量時間。

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