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

Android 介面美化入門(十六):Style 與 Theme 的分工與實作

編輯:Android 開發實例

愈來愈多網路服務把客戶端搬上 Android,而在功能相近的情況下,使用者最後往往是被「好不好看、順不順手」說服的。Style 與 Theme 正是 Android 用來統一視覺語言、降低重複勞動的兩項機制。

一、Theme 與 Style 的界線

  • Theme 作用在「視窗(Window)」層級,改變的是整個 Activity 或整個程式的外觀基調。
  • Style 作用在「元件」層級,針對特定的控制項或 Layout 指定外觀。

兩者的定義格式其實一樣,差別只在套用範圍。撰寫方式也相同:在 res/values/ 底下新增 XML,根節點為 <resources>,每個樣式用 <style> 宣告並取一個全域唯一的名字,可選擇用 parent 屬性繼承既有樣式。

二、先看系統怎麼寫

Android 原始碼裡的 themes.xml 與 style.xml(位於 \base\core\res\res\values\)收錄了大量內建樣式。與其從零開始,不如從中挑一個接近需求的樣式再繼承修改。以下摘自系統 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 基本上只是在定義每個控制項的預設外觀而已。不過實務上不太建議把控制項樣式改得太離譜,耐看、耐長時間使用的設計才是好設計。另外,控制項樣式大量用到 9.png 這種可延伸圖形,想學好它就到 \base\core\res\res\drawable-hdpi 翻一翻系統內建的範例。

因此,若真要深入研究 Style 與 Theme,強烈建議把 Android 的 base.git 抓下來當字典查。

三、自訂 Theme

以下範例繼承內建的 Theme.Light,改成全螢幕並放大標題列,同時自訂 WindowTitle 加入陰影:

<?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 套用這個主題,有兩條路:在程式碼中呼叫 setTheme(R.style.Theme),或在 AndroidManifest.xml 裡針對該 Activity 標籤指定。前者務必放在 setContentView() 之前,否則不會生效;一般情況下用 Manifest 宣告更乾脆。

四、自訂控制項 Style

接著為 TextView、EditText、Button 各定義一份樣式(背景沿用系統的 9.png,其 XML 定義位於 \base\core\res\res\drawable\ 之下):

<?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>

五、在 Layout 中引用

布局檔只要用 style="@style/..."​ 掛上去即可,不需要在每個控制項裡重複寫屬性:

<?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"
        style="@style/TextView" />

    <EditText
        android:id="@+id/EditText01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/EditText"
        android:text="類似 Button 的 EditText" />

    <EditText
        android:id="@+id/EditText02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="普通的 EditText" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/Button"
        android:text="類似 EditText 的 Button" />
</LinearLayout>

範例刻意讓 EditText 長得像 Button、Button 長得像 EditText,用來說明 Style 的威力:同一份版面結構,換一組樣式定義,視覺語言就完全變了。這也是 Style 最實際的價值——把「長相」從「結構」裡抽離出來,讓設計調整不必驚動版面。

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