天天看點

Android 開發之:Tools 屬性

直到今天建立項目的時候, 出現tools:showIn="@layout/activity_main"才想起要學習記錄下 Tools屬性的相關使用。這個Tools 是如此好使用,我卻未能善用,簡直抱歉。它提高Android 應用開發的效率、還可以提高代碼品質。

Android 開發之:Tools 屬性

==

首先介紹布局檔案中的tools 屬性。

如果用 Android Studio 建立一個簡單的示例項目,在生成的布局檔案中會有這麼一行内容:

xmlns:tools=” http://schemas.android.com/tools

在** tools 命名空間下定義了一些屬性就是我們要介紹的 tools 屬性。 顧名思義,這些屬性都是為編輯工具準備的,具體來說就是 Android Studio 和布局檔案編譯器(AAPT**)。 在開發程式的時候,通過這些屬性來告訴編輯器一些額外的資訊。

tools:ignore

這個屬性是為** Lint 準備的,屬性的取值為逗号分隔的 Lint 問題 ID**。告訴 Lint 在檢測代碼的時候,這些問題不用檢測。例如:

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

tools:targetApi

這個屬性也是為 Lint 準備的。和 Java 注解 @TargetApi 一樣。取值可以為 Android 版本代号或者對應的整數值。例如:

<GridLayout tools:targetApi=”ICE_CREAM_SANDWICH” >
           

tools:locale

Lint 和 Android Studio 都會使用該屬性。取值為 語言和地區縮寫。如果設定該值為非英文類型,則 Studio 不會執行拼寫檢查。該屬性通常出現在資源檔案的根元素上。用來告訴 Lint 該檔案應該用在那種語言環境中。 例如:values/strings.xml 檔案 設定該屬性為 es

<resources xmlns:tools=”http://schemas.android.com/tools” tools:locale=”es”>
           

上面的設定告訴工具,預設的檔案夾中的資源為西班牙語而不是英語。

tools:context

Lint 和 Android Studio 都會使用該屬性。出現在布局根元素中,用來告訴工具該布局檔案和哪個 Activity 關聯。這樣在設計的時候,布局編輯器會使用相關聯的 Activity 的 theme 來渲染該布局檔案。取值為 manifests 檔案中的 activity name 值一樣。例如:

<android.support.v7.widget.GridLayout
  xmlns:android=”http://schemas.android.com/apk/res/android” 
  xmlns:tools=”http://schemas.android.com/tools”
  tools:context=”.MainActivity” … >
</android.support.v7.widget.GridLayout>```
**tools:layout**
該屬性由 Android Studio 使用。通常用在 <fragment> 元素中,用來告訴編輯器該 fragment 使用的是哪個布局檔案。
```code=xml 
<fragment
  android:name=”com.example.master.ItemListFragment”
  tools:layout=”@android:layout/list_content” />
           

tools:listitem / listheader / listfooter

Android Studio 使用該屬性來渲染清單元素。可以用在 AdapterView 的子 View 中。例如 <ListView>、<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″ />
           

tools:showIn

Android Studio 使用該屬性。通常用在被其他布局檔案引用的<include> 布局檔案中。告訴編輯器該布局檔案用在另外一個布局檔案中。例如

<?xml version=”1.0″ encoding=”utf-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” />
           

tools:menu

Android Studio 使用該屬性。用在布局檔案的根元素中,告訴編輯器在 ActionBar 上的菜單内容。取值為定義的 menu 的 id,多個 id 用逗号分隔;還可以使用定義 menu 的 xml 檔案的名字。

例如:

<?xml version=”1.0″ encoding=”utf-8″?>
<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

Android Studio 使用該屬性。用在布局檔案的根元素中,告訴編輯器在 ActionBar 上的導航模式。取值為 “standard”、 “list” 和”tabs” 之一。

<?xml version=”1.0″ encoding=”utf-8″?>
<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” />
           

除了上面這些屬性以外,标準的 Android 布局屬性都可以當做 tools 屬性來使用。布局編輯器用這些屬性來選擇布局檔案。這些屬性被稱之為 “設計時布局屬性”,他們隻被布局編輯器使用,編譯後的代碼中不存在這些内容。

例如:常用于即時預覽填充 tools:text="weixiong_wang"等(最常用,最好用)。

參考:

http://blog.chengyunfeng.com/?p=755&utm_source=tuicool&utm_medium=referral

繼續閱讀