編輯:關於 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 的威力:外觀與行為徹底分離。真正上線的專案裡,把樣式集中管理、統一調整,會省下大量重複勞動。
Android RxJava 使用介紹(二):操作符(上)
上一篇我們用一個簡單的例子展示了 RxJava 的基本用法,相信大家已對它有了初步認識。由於上次只是點到為止,也許你還會覺得 RxJava 有點名不副實。那麼這一篇,我們
Android 繪製圓形圖片的兩種經典做法
頭像裁成圓形是很常見的需求。Android 裡實現的方式不少,最經典的莫過於 BitmapShader(著色器)與 Xfermode(圖層相交模式)這兩種。先看成果:
Android 高仿雅虎天氣(一):項目緣起與效果展示
引言: 記得去年下半年我曾上傳過一份代碼(超逼真仿雅虎天氣界面),但那僅僅只是一個界面,還存在幾個比較嚴重的 bug。印象最深的一個是:那個可以換位置的 ListView
Android 優化下載(四):用 DDMS 網絡流量工具找出耗電區段
使用 DDMS 網絡流量工具識別關注的區間 Android 的 DDMS(Dalvik Debug Monitor Server)內建了詳細的網絡使用量頁籤,讓開發者能夠