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

Android 官方文件翻譯:tools 命名空間屬性全解

編輯:關於 Android 程式設計

Android 提供了一個專用的 XML 命名空間,讓開發工具得以在資源檔裡記錄額外資訊,而這些資訊在打包時會被剝除,不影響執行期效能與 APK 體積。

命名空間 URI 是 https://schemas.android.com/tools,慣例綁定到 tools: 前綴:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    ...
</FrameLayout>

以下逐一說明目前常用的 tools 屬性(注意:這批屬性會隨版本演進而增減)。

tools:ignore

可放在任何元素上,值為逗號分隔的 lint 問題 ID 清單,表示此元素及其所有子元素遞迴忽略這些檢查。

<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>

用途:Lint。

tools:targetApi

效果等同 Java 的 @TargetApi 註解,用來標示此元素需要某個 API level(整數或代碼名稱)以上才適用。

<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >

用途:Lint。

tools:locale

設定在資源檔的根元素上,對應語言或地區,讓工具知道檔案內字串假設是哪種語言。例如:

<resources xmlns:tools="https://schemas.android.com/tools"
    tools:locale="es">

如此一來,工具便知道預設 values 資料夾裡的字串是西班牙文而非英文。

用途:Lint、Studio(用於停用非英文資源檔的拼字檢查)。

tools:context

通常設在版面 XML 的根元素,標明這個版面關聯到哪個 Activity。因為主題定義在 Manifest 且與 Activity 綁定,Layout Editor 便得以推斷預設主題。可仿照 Manifest 用點號開頭簡寫:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    tools:context=".MainActivity" ... >

用途:Studio 與 Eclipse 的版面編輯器、Lint。

tools:layout

設在 <fragment> 標籤上,指定設計檢視時要渲染的版面(執行期實際顯示哪個版面,由 Fragment 邏輯決定)。

<fragment
    android:name="com.example.master.ItemListFragment"
    tools:layout="@android:layout/list_content" />

用途:Studio 與 Eclipse 的版面編輯器。

tools:listitem / listheader / listfooter

用於設計期的 ListView(以及 GridView、ExpandableListView 等 AdapterView 子類),指定列表項、頁首與頁尾的版面。工具會填入假資料,讓你看得到列表的大致樣貌。

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:listitem="@android:layout/simple_list_item_2" />

用途:Studio 與 Eclipse 的版面編輯器。

tools:showIn

設在一個會被 <include> 的版面之根元素上,指向包含它的其中一個版面。這樣被包含的版面在設計時會連同外層版面一起渲染,讓你能「在情境中」編輯。需要 Studio 0.5.8 以上版本。

<TextView xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:showIn="@layout/activity_main" />

用途:Studio 的版面編輯器。

tools:menu

設在版面根元素,用來指定 Action Bar 上要顯示的選單。Studio 預設會去關聯 Activity(透過 tools:context)的 onCreateOptionsMenu() 推測,這個屬性讓你能手動覆寫。值為逗號分隔的 ID 清單,不加 @id/ 之類前綴,也可直接寫選單 XML 的檔名(省略 .xml)。需要 Studio 0.8.0 以上版本。

<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:menu="menu1,menu2" />

用途:Studio 的版面編輯器。

tools:actionBarNavMode

同樣設在根元素,指定 Action Bar 的導覽模式,可選值為 standard、list、tabs。需要 Studio 0.8.0 以上版本。

<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" />

用途:Studio 的版面編輯器。

補充:設計期屬性

除了上述 tools 專屬屬性,版面中任何既有的 Android 屬性都能加上 tools: 前綴,做為「僅設計期生效」的覆寫值。最典型的用法是 tools:text——讓你在預覽時看到示意文字,卻不影響執行期顯示的內容。這對 ListView 項目、自訂 View 的預覽特別有用。

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