Android -- tools
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" ></FrameLayout>
這個tools標籤主要是為adt外掛程式使用的。他裡面的很多屬效能在很大程度上方便我們的開發,但是並不會影響我們最終產生的apk包。比如大家在寫一個介面的時候一般都會給Textview寫上text的值,然後在開發完畢的時候再刪除他,這個操作就很麻煩,但是現在你就可以。 tools:ignore 此屬性可以在任何 XML 元素上設定,它是一個逗號分割的lint 問題ID的列表,表示了應該要在此元素或它的任何子項目上遞迴忽略的lint問題的ID。 <string name="show_all_apps" tools:ignore="MissingTranslation">All</string>tools:context
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" />
tools:context=".MainActivity"這一句不會被打包進APK。只是ADT的Layout Editor在你當前的Layout檔案裡面設定對應的渲染上下文,說明你當前的Layout所在的渲染上下文是activity name對應的那個activity,如果這個activity在manifest檔案中設定了Theme,那麼ADT的Layout Editor會根據這個Theme來渲染你當前的Layout。僅用於給你看所見即所得 (WYSIWYG)的效果而已。(One more thing: The "tools" namespace is special. The android packaging tool knows to ignore it, so none of those attributes will be packaged into the APK. We're using it for extra metadata in the layout. It's also where for example the attributes to suppress lint warnings are stored -- as tools:ignore.) tools:targetApi 此屬性像 Java 類中的 @TargetApi 批註解一樣: 它允許您指定一個 API 層級,可以是整數或代碼名稱,表示此元素需要在此層級之上運行。
<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" ......... >tools:text<TextView android:text="text" tools:text="tools text" ......... >
tools:text,其實就是給ADT用的,用於在design頁面能夠預覽到這個屬性的值,但是當實際上啟動並執行時候是看不到這個值的。 tools:listitem / listheader / listfooter 是給ADT來讓你預覽listview布局的。 <ListeView tools:listview="@android:layout/simple_list_item_1" ......... >tools:locale 此屬性可以設定在資源檔的根項目上,並且應該對應於一種語言或一個地區。這會讓工具知道檔案的字串被假定為哪種語言(地區)中的。例如, values/strings.xml 可以有此根項目: <resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">tools:layout 此屬性通常設定在一個 標籤中,用來記錄在設計時你想看到渲染的布局 (在運行時,將由這個標籤所列的fragment的類的操作所決定)。
<fragment android:name=".MyFragment" tools:layout="@android:layout/list_content" />tools:showIn
該屬性設定於一個被其他布局的布局的根項目上。這讓您可以指向包含此布局的其中一個布局,在設計時這個被包含的布局會帶著周圍的外部布局被渲染。這將允許您“在上下文中”查看和編輯這個布局。
<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" />tools:menu
這個屬性在布局的根項目上設定,用於配置在 Action Bar中顯示的菜單。Android Studio 通過查看這個布局檔案所連結的activity(通過 tools:context)裡的onCreateOptionsMenu()方法,嘗試找出哪些菜單在 ActionBar 中使用。它允許重寫哪個搜尋和顯示聲明的菜單用於顯示。它的值是逗號分隔的 id 列表 (沒有 @id/ 或任何這類首碼)。還可以使用沒有.xml 副檔名的菜單xml檔案的名稱。 x
<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" />tools:actionBarNavMode
這個屬性在布局的根項目上設定,用於配置 Action Bar 使用的導航模式。可能的值包括:“standard”,“list”和“tabs”。
<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" />