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

深入解析 Android 布局中的 xmlns:tools 命名空間

編輯:關於 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 這類屬性加不加都不影響編譯,有些人一律不理會警告也無妨,可依團隊規範決定。

版面預覽相關屬性

以下幾個才是真正能提升開發速度的:

  1. tools:context
  2. tools:menu
  3. tools:actionBarNavMode
  4. tools:listitem/listheader/listfooter
  5. tools:showIn
  6. tools:layout

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: 填假資料的習慣,能省下不少上線後才發現的尷尬。

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