1. 程式人生 > >android官方技術文件翻譯——工具屬性

android官方技術文件翻譯——工具屬性

本文譯自androd官方技術文件《Tools Attributes》:http://tools.android.com/tech-docs/tools-attributes

本文地址:http://blog.csdn.net/maosidiaoxian/article/details/41510581。轉載請註明出處。翻譯如有錯訛,敬請指正。

工具屬性

Android 有一個專用的XML名稱空間,用於使工具可以記錄XML檔案裡的資訊,並且在打包程式的進行把資訊剝離到不會帶來執行時期和下載大小的負面影響的程度。 這個名稱空間的 URI 是 http://schemas.android.com/tools,並且它通常被繫結到 
tools: 字首中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >     .... 這份文件記錄了我們當前使用的工具屬性。(注: 這些屬性可能會隨著時間在以後中改變。)

tools:ignore

此屬性可以在任何 XML 元素上設定,它是一個逗號分割的lint 問題ID的列表,表示了應該要在此元素或它的任何子元素上遞迴忽略的lint問題的ID。
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>

使用: Lint

tools:targetApi

此屬性像 Java 類中的 @TargetApi 批註解一樣: 它允許您指定一個 API 級別,可以是整數或程式碼名稱,表示此元素需要在此級別之上執行。

    <GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >

使用: Lint

tools:locale

此屬性可以設定在資原始檔的根元素上,並且應該對應於一種語言或一個地區。這會讓工具知道檔案的字串被假定為哪種語言(區域)中的。例如, 
values/strings.xml 可以有此根元素:

<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">
現在我們知道預設值資料夾中的字串用的語言是西班牙語,而不是英語。

使用: Lint, Studio (以在非英語資原始檔中禁用拼寫檢查)

tools:context

這個屬性通常在一個佈局XML檔案的根元素中設定,記錄了這個佈局關聯到哪一個activity(因為顯然一個佈局在設計時可以被多個佈局使用)(例如它會用於佈局編輯器中以推斷預設的主題,由於主題定義在Manifest中,並與activity而不是佈局相關聯。你可以和在manifests中一樣使用點字首,來指定activity類,而不需要使用完整的程式包名作為字首。 <android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://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>(或其他 AdapterView 的子類,比如 <GridView>,<ExpandableListView>等) 來指定佈局使用的列表項,以及列表頭部和列表底部。該工具將填充假的資料,以顯示一個有一些類似內容的列表。     <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="http://schemas.android.com/apk/res/android"     xmlns:tools="http://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中顯示的選單。Android Studio 通過檢視這個佈局檔案所連結的activity(通過 tools:context)裡的onCreateOptionsMenu()方法,嘗試找出哪些選單在 ActionBar 中使用。它允許您重寫哪個搜尋和顯示宣告的選單用於顯示。它的值是逗號分隔的 id 列表 (沒有 @id/ 或任何這類字首)。您還可以使用沒有.xml 副檔名的選單xml檔案的名稱。需要 Studio 0.8.0 或更高版本。 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://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="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent" tools:actionBarNavMode="tabs" /> 使用:Studio 的佈局編輯器

其他屬性: 設計時屬性

在佈局中,任何其他屬性可以是一個內建的 Android 屬性別名。例如,這可以讓您設定僅設計時的替換文字,用於工具中而不是執行時。詳細資訊,請參閱設計時佈局屬性