編輯:Android 開發實例
愈來愈多網路服務把客戶端搬上 Android,而在功能相近的情況下,使用者最後往往是被「好不好看、順不順手」說服的。Style 與 Theme 正是 Android 用來統一視覺語言、降低重複勞動的兩項機制。
兩者的定義格式其實一樣,差別只在套用範圍。撰寫方式也相同:在 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.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 宣告更乾脆。
接著為 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>
布局檔只要用 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 最實際的價值——把「長相」從「結構」裡抽離出來,讓設計調整不必驚動版面。
Android TextView 控件詳解:屬性、方法與使用要點
文本控件的兩大主力 在 Android 開發中,凡是需要與「文字」打交道的地方,都離不開兩個控件:TextView 與 EditText。前者負責展示,後者負責收集輸入。
搞懂點九圖片的「內容顯示區域」是什麼意思
點九圖(.9.png)的拉伸區域由左上方那一格標記,這個觀念大多數人都懂;但右側那一格所定義的「內容顯示區域」究竟是什麼,就常常讓人卡住。 為什麼需要點九圖 智慧型手機普
Android 開發技巧:打造專屬風格的自定義菜單
Android SDK 本身提供了一套建立菜單的預設機制,這種方式做出來的菜單功能上雖然完整,但外觀實在有點「土」。當一個畫面絢麗的應用配上一個老氣的菜單,整體質感就會大
Android 設定裡的隱藏彩蛋是怎麼做出來的(2.3 原始碼解析)
工程師偶爾也會在系統裡藏點小趣味。Android 的「設定」中就有這樣一處:連續快速點擊「Android 版本」,會跳出一張圖;在 2.3 上是那張著名的安卓機器人塗鴉: