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

Android Style 與 Theme 的差異與實作範例

編輯:Android 開發實例

Style 與 Theme 的編寫,直接影響整個程式界面的美觀與一致性。用得好的話,能讓所有畫面呈現統一而精緻的視覺語言——概念上相當於 Web 開發中的 CSS。

兩者都是資源,存放於 res/values 資料夾下,也都以 <style> 元素宣告。

Style 與 Theme 的分別

  • Style:一組格式化屬性的集合,作為單一單位套用在版面 XML 的某個元件上。例如定義一種風格來指定文字大小與顏色,再套用到某個 View 實例。
  • Theme:同樣是一組格式化屬性,但套用的範圍是整個應用或某個 Activity。例如定義一個 Theme 來設定視窗框與面板的前景背景顏色、選單文字的大小與色彩,就能一次套用到程式中所有 Activity。

一句話總結:Style 作用在 View,Theme 作用在 Activity 或 Application。Theme 不能套用在單一 View 上。

XML 結構

每個 Style 或 Theme 都要在 <style> 上給一個全域唯一的 name,也可選擇加上 parent 屬性來表明繼承來源。在 <style> 內部宣告一個或多個 <item>,每個 item 透過 name 屬性指定要覆寫的屬性,值則寫在元素內容中。

Style 範例

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="SpecialText" parent="@style/Text">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">#008</item>
    </style>
</resources>

套用到單一 View:

<EditText id="@+id/text1"
    style="@style/mytext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello, World!" />

不繼承任何父風格的寫法:

<style name="SpecialText">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">#EC9237</item>
</style>
<style name="SpecialText2">
    <item name="android:textSize">26sp</item>
    <item name="android:textColor">#FF7F7C</item>
</style>

套用:

<TextView
    style="@style/SpecialText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
/>

Theme 範例

Theme 同樣在 <style> 中宣告、同樣方式引用,差別在於它是透過 AndroidManifest 的 <application> 或 <activity> 元素套用。

<style name="CustomTheme" parent="android:Theme.Black">
    <item name="android:windowNoTitle">true</item>
    <item name="android:textColor">#FFFF0000</item>
</style>

套用到整個應用:

<application android:theme="@style/CustomTheme">

只套用到某個 Activity:

<activity android:theme="@android:style/Theme.Dialog">

也可以在程式碼中於 setContentView() 之前呼叫 setTheme()(順序錯了就沒有效果):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_Light);
    setContentView(R.layout.linear_layout_3);
}

@ 與 ? 的差別

這兩個符號經常被混淆。@ 表示引用一個已定義完成的資源(可能在前面的 XML、其他專案或 Android 框架中);? 則表示引用一個由目前 Theme 動態決定的值。例如下面這段寫法中,panelTextColor 的顏色會跟隨 panelForegroundColor,使用者換主題時兩者自動同步:

<item name="panelForegroundColor">#FF000000</item>
<item name="panelTextColor">?panelForegroundColor</item>

這項技巧只能用在 XML 資源中。

實用建議

Android 內建了幾套主題可直接套用,例如 @android:style/Theme.Dialog 能讓 Activity 看起來像對話框。若你喜歡某個內建主題但想稍作修改,最省事的做法是把它設為 parent 再繼承覆寫,而不是從零開始:

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:textColor">#FF0000FF</item>
</style>

實務上有兩點值得注意。第一,android: 前綴代表屬性來自系統框架;若使用 AndroidX/Material Components,自訂屬性通常沒有前綴(如 colorPrimary),混用會造成資源找不到的錯誤。第二,原文範例中的 android:fromAlpha、android:toAlpha、android:testSize 並非有效的 View 屬性,直接抄用會編譯失敗——淡入淡出效果應改用 android:alpha 搭配動畫資源實作。

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