編輯:關於 Android 程式設計
寫 Android 時我們的注意力多半被功能牽著走,但介面和外觀的重要性完全不亞於功能。調整外觀大致有兩條路:一是直接在 XML 裡逐個修改 View 屬性;二是建立自訂的樣式(Style)與主題(Theme)。
熟悉網頁開發的人可以這樣類比:前者像寫 inline CSS,後者則像抽出一份 style sheet。顯然,只有當畫面元件寥寥可數時,第一種做法才勉強可行;專案一長大就會變成維護噩夢。本文就從 Style 一路談到 Theme,把整個流程走一遍。
Style 是套用在 UI 元件上的,所以先開一個空白 Activity,丟兩個 View 進布局:
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="#009688"
android:id="@+id/box1" />
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00BCD4"
android:layout_margin="5dp"
android:id="@+id/box2" />
可以看到 layout_width 與 layout_margin 在兩個 View 中被重複寫了一次又一次。抽出樣式的快捷方式:對 View 按右鍵,選擇 Refactor > Extract > Style。
跳出的對話框裡可以替樣式命名,並勾選要納入哪些屬性。這裡命名為 MyBox,除了 background(因為兩個方塊顏色不同)其餘全選。

按下 OK,第一個 View 立刻瘦身:
<View android:background="#009688" android:id="@+id/box1" />
它現在多了一個指向 MyBox 的 style 屬性。打開 res/values/styles.xml 就能看到定義:
<style name="MyBox">
<item name="android:layout_width">100dp</item>
<item name="android:layout_height">100dp</item>
<item name="android:layout_margin">5dp</item>
</style>
定義好之後,樣式可以套用到任何 View。例如讓第二個 View 也共用:
<View android:background="#00BCD4" android:id="@+id/box2" />
套用完成,兩個 View 的外觀就一致了:

Android 允許在既有樣式上派生子樣式,語法有兩種。
隱式語法用「.」當分隔符。例如要從 MyBox 派生 TEAL 與 CYAN:
<style name="MyBox.TEAL">
<item name="android:background">#009688</item>
</style>
<style name="MyBox.CYAN">
<item name="android:background">#00BCD4</item>
</style>
不難推測,這兩個子樣式除了繼承 MyBox 的全部屬性,各自又多了 android:background。
顯式語法則透過 parent 屬性指定父樣式名稱:
<style name="TealBox" parent="MyBox">
<item name="android:background">#009688</item>
</style>
套用派生樣式跟套用一般樣式沒有任何差別:
<View android:id="@+id/box1" /> <View android:id="@+id/box2" />
實務上的慣例是:繼承自己寫的樣式時用隱式語法,繼承系統樣式時用顯式語法。
到目前為止,樣式都只作用在 Activity 內部的 View。一旦把樣式套用到整個 activity 或 application,它就換了個身分——主題(Theme)。
用新版 Android Studio 建立的專案,預設都採用名為 AppTheme 的主題。它繼承自 AppCompat,是個影響面極廣的主題,幾乎涵蓋所有常用元件的外觀。定義在 styles.xml 裡:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
由於 AppTheme 遵循 Material Design,想快速做出一個符合規範的配色,以它為 parent 是很安全的選擇;當然也可以直接用 Theme.AppCompat。
主題本質上就是樣式,寫 XML 完全可以,但 Android Studio 提供了圖形化的主題編輯器,能省下不少工。開啟方式:Tools 選單 > Android > Theme Editor。視窗右側是各項色票與建立新主題的入口,左側即時預覽套用後的樣子。

點開 Theme 下拉選單,選擇 Create New Theme,命名為 MyTheme 後確認。

此時 styles.xml 會多出一行:
<style name="MyTheme" parent="AppTheme" />
接著用編輯器調整 colorPrimary、colorPrimaryDark 與 colorAccent。點擊 colorPrimary 按鈕會彈出色盤,選色時務必替它取個新名字,否則編輯器會直接覆寫 AppTheme 原本的色彩定義,把預設主題也一起改壞。

另外兩色的操作相同,而且編輯器會依你選定的 colorPrimary 自動推薦搭配的深色系與強調色。調整後的定義長這樣:
<style name="MyTheme" parent="AppTheme" >
<item name="colorPrimary">@color/colorPrimaryMyTheme</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkMyTheme</item>
<item name="colorAccent">@color/colorAccentMyTheme</item>
</style>
為了看出主題的實際差異,先往 Activity 裡補一批常見元件——普通 Button、無邊框 Button、彩色 Button、CheckBox、RadioButton、Switch、SeekBar、TextView 與 EditText:
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="normal" android:id="@+id/normal_button" />
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="borderless" android:id="@+id/borderless_button" />
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="colored" android:id="@+id/colored_button" />
<CheckBox android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="New CheckBox" android:id="@+id/checkBox" />
<RadioButton android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="New RadioButton" android:id="@+id/radioButton" />
<Switch android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="New Switch" android:id="@+id/switchButton" />
<SeekBar android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/seekBar" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="New Text" android:id="@+id/textView" />
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/editText" android:hint="Input" />
加入後的畫面如下:

讀過 Material Design 規範的人會立刻認出:此時 colorPrimary 與 colorPrimaryDark 用的是靛藍,colorAccent 是粉紅,這些都是 Android Studio 的預設值,實際 hex 藏在專案的 res/values/colors.xml 裡。
要讓 Activity 改用自訂主題,打開 manifest,在該 Activity 節點上補一個 android:theme:
<activity android:name=".MainActivity" android:theme="@style/MyTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
同樣的屬性寫在 application 節點上,就能一次套用到整個 App。套用之後再回頭看畫面,差異會非常明顯:

樣式與主題是「一處修改、處處生效」的工具,這既是它的價值也是它的風險。自訂主題時最好一律以 AppTheme 為 parent 並只覆寫必要項目,避免把 AppCompat 內部依賴的屬性改掉而引發莫名的渲染問題。另外,多數使用者早已習慣 Material Design 的視覺語言,配色與元件樣式偏離太遠反而會造成認知負擔——客製化在精不在多。
Android 平台終端模擬器與 SSH 客戶端實測筆記(附幾個實用 Tips)
自從用 Linux Deploy 在平板裡部署好 Kali Linux 之後,我拿到平板的時間有一半以上都耗在終端模擬器(Terminal Emulator)裡。對我來說
Android 動畫:讓齒輪永不停歇地轉動
很多 loading 或裝飾性動畫裡都會用到「轉個不停的齒輪」效果。實現起來其實很簡單,用 RotateAnimation 就能讓齒輪無限勻速旋轉。 (這裡是靜態圖,實
Android 自定義 View(三):深入解析控件測量 onMeasure
先把自定義 View 的套路搬上來,一切按流程辦事:繼承 View 並覆蓋構造方法 → 自定義屬性 → 重寫 onMeasure 測量寬高 → 重寫 onDraw 繪製
Android 位移傳感器全面解析
Android 平台提供了一系列傳感器,讓你能監測設備的運動。其中加速度計和陀螺儀總是基於硬件,而重力、線性加速度、旋轉矢量這三種傳感器則既可用硬件也可用軟件實現。例如,