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

Android 入門:Style 與 Theme 的用法實例解析

編輯:關於 Android 程式設計

現在有越來越多的網路服務把客戶端搬上 Android,而為了留住使用者,這些客戶端在視覺上多半下過功夫:佈局合理、外觀耐看。這背後的關鍵之一,就是 Android 的 Style 與 Theme 機制。Android 把它分成兩個層次:

1. Theme 作用在整個窗體級別,改變視窗的外觀;
2. Style 作用在具體的控件或 Layout 級別,改變單一元素的樣式。

系統內建的 themes.xml 與 style.xml(位於 /base/core/res/res/values/)已經定義了大量現成樣式,實務上的建議是:先從裡面挑一個接近需求的,再繼承它做修改,而不是從零開始。以下這段屬性取自系統本身的 themes.xml,是 Theme 中最常見的幾項:

 <!-- 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 的屬性編輯器(Properties)掃一眼便知;系統的 style.xml 其實也只是為每個控件定義了預設外觀而已。不過有個原則值得牢記:控件的 Style 不建議大改,耐看的樣式才能讓使用者長期使用而不厭。另外,Style 設計裡大量用到 9.png,想真正弄懂它,可以到 /base/core/res/res/drawable-hdpi 翻翻系統內建的範例。

注意:為了深入研究 Style 與 Theme,強烈建議把 Android 的 base.git 下載下來對照著看。

本文程式的執行效果:

themes.xml,這裡自訂了 WindowTitle:

 <?xml version="1.0" encoding="UTF-8"?> <resources> <!--繼承Android內置的Theme.Light,位於/base/core/res/res/values/themes.xml --> <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),也可以直接在 Manifest 裡設定:

styles.xml,其中 background 預設使用 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> 

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> 

注意範例裡 EditText 與 Button 互換了背景——這正是 Style 的威力:外觀與行為徹底分離。真正上線的專案裡,把樣式集中管理、統一調整,會省下大量重複勞動。

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