編輯:關於 Android 程式設計
前言
寫 Android 版面時,Android Studio 右側的預覽視窗很方便,但有個尷尬之處:有些文字是寫在 Activity 的 Java 程式碼裡才設上去的,XML 裡的 TextView 一片空白,預覽自然什麼都看不到。

為了解決這件事,很多人會先在 XML 塞一個假文字:
<TextView android:id="@+id/text_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.Title" android:layout_margin="@dimen/main_margin" android:text="I am a title" />
然後在心裡跟自己說「等等寫完就刪掉」。問題是——通常就忘了,於是「I am a title」跟著上線,使用者真的會看到它。
用 tools 命名空間,別跟自己過不去
這個問題有正解。先在根節點宣告命名空間:
xmlns:tools=https://schemas.android.com/tools
tools 的語意是:這些屬性只在設計階段有效,打包時會被完全忽略,不會進入 APK。所以把 android:text 換成 tools:text,預覽看得到、正式版看不到:
<TextView android:id="@+id/text_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.Title" android:layout_margin="@dimen/main_margin" tools:text="I am a title" />
規則很單純:任何標準 android 屬性,把前綴換成 tools: 就能覆蓋成「僅設計時有效」。
tools 屬性的兩大類
大致可分為「影響 Lint 提示」與「關於版面預覽」兩種。上面示範的屬於後者,以下先看 Lint 相關的三個。
Lint 相關屬性
tools:ignore tools:targetApi tools:locale
tools:ignore
用來讓 Lint 對特定警告閉嘴。例如這個 ImageView:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/margin_main" android:layout_marginTop="@dimen/margin_main" android:scaleType="center" android:src="@drawable/divider" />
Lint 會抱怨缺少 android:contentDescription。若這張圖純屬裝飾,加上 ignore 即可:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/margin_main" android:layout_marginTop="@dimen/margin_main" android:scaleType="center" android:src="@drawable/divider" tools:ignore="contentDescription" />
tools:targetApi
當 minSdk 是 15,卻用了 API 21 才有的 RippleDrawable:
<ripple xmlns:android="https://schemas.android.com/apk/res/android" android:color="@color/accent_color" />
Lint 會跳版本相容警告。若你已經在程式碼中做好版本判斷,可以標註 targetApi 告知 Lint:
<ripple xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:color="@color/accent_color" tools:targetApi="LOLLIPOP" />
tools:locale
res/values/strings.xml 預設會跑拼字檢查,非英文內容容易被畫上紅線。用 locale 宣告實際語言即可:
<resources xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" tools:locale="it"> <!-- Your strings go here --> </resources>
注意:Lint 這類屬性加不加都不影響編譯,有些人一律不理會警告也無妨,可依團隊規範決定。
版面預覽相關屬性
以下幾個才是真正能提升開發速度的:
tools:context
它關聯一個 Activity,IDE 便知道預覽時該套用哪個主題,同時在 Java 檔中能直接「Go to Related Files」跳到對應版面。

值為 Activity 的完整類別名稱:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.android.example.MainActivity"> <!-- ... --> </LinearLayout>
tools:menu
指定預覽時要掛在哪個選單上,它會顯示於版面根節點(action bar 的位置)。

預覽其實很聰明:若已用 tools:context 關聯 Activity,它會自動去讀該 Activity 的 onCreateOptionsMenu 來顯示選單。而 menu 屬性可以覆蓋這個行為,多個選單以逗號分隔:
tools:menu="menu_main,menu_edit"
不想顯示就給空字串:
tools:menu=""
要注意主題為 Theme.AppCompat 時此屬性不生效。
tools:actionBarNavMode
用來預覽 app bar 的導航模式,可選值:
standard tabs list
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:actionBarNavMode="tabs" />
同樣地,主題為 AppCompat(r21 以上)、Material,或版面裡含 Toolbar 時此屬性無效,只有 holo 主題吃這一套。
listitem/listheader/listfooter
顧名思義,用來預覽 ListView、ExpandableListView、GridView 等元件的表頭、表尾與單項樣貌:
<GridView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" tools:listheader="@layout/list_header" tools:listitem="@layout/list_item" tools:listfooter="@layout/list_footer" />
tools:layout
告訴 IDE 這個 fragment 標籤在預覽時該長什麼樣子:
<fragment xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:id="@+id/item_list" android:name="com.example.fragmenttwopanel.ItemListFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" tools:layout="@android:layout/list_content" />

tools:showIn
寫在被 <include> 的版面根節點上,指向包含它的父版面。如此一來,這個子版面在預覽時會帶著外層結構一起被渲染,讓你能「在情境中」編輯它,而不是孤零零地看一個片段。此屬性需要 Android Studio 0.5.8 以上版本。
總結
tools 命名空間的價值在於把「設計期資訊」與「執行期程式碼」乾淨地切開:預覽該有的假資料照給,但一行都不會進到 APK。養成用 tools: 代替 android: 填假資料的習慣,能省下不少上線後才發現的尷尬。
Android 打造通用的帶進度條 WebView 元件
App 中載入網頁時,從按下連結到內容真正顯示出來總會有一段空窗。這段時間如果畫面一片空白,使用者很容易以為卡住了,所以加上一個進度條幾乎是必要之舉。本文把它封裝成一個自
Fragment 與 Activity 的通信方式及 Fragment 生命週期詳解
一、Fragment 與 Activity 的通信 最推薦的做法是「接口回呼」,五步搞定: 在 Fragment 中聲明一個接口 在 Activity 中實現該接口 在
Android ListView 基礎用法:從資料綁定到點擊事件
ListView 的定位ListView 是 Android 中非常常見的列表元件,適合用來展示大量結構化資料。它既可以使用系統預設樣式,也可以透過自定義 Layout
Android 高手進階教程(二十六):打造超仿 Path 的展開式菜單
Hey~大家好,出來創業已經快三個月了,一切還算順利。前陣子利用業餘時間做了一個問答類網站 YQMA,想打造中國版的 Stack Overflow,哈哈,只是先妄想一下,