直到今天創(chuàng)建項目的時候, 出現(xiàn)tools:showIn="@layout/activity_main"才想起要學(xué)習(xí)記錄下 Tools屬性的相關(guān)使用请垛。這個Tools 是如此好使用宗收,我卻未能善用,簡直抱歉混稽。它提高Android 應(yīng)用開發(fā)的效率荚坞、還可以提高代碼質(zhì)量。
首先介紹布局文件中的tools 屬性各淀。
如果用 Android Studio 創(chuàng)建一個簡單的示例項目碎浇,在生成的布局文件中會有這么一行內(nèi)容:
xmlns:tools=”http://schemas.android.com/tools”
在** tools 命名空間下定義了一些屬性就是我們要介紹的 tools 屬性奴璃。 顧名思義城豁,這些屬性都是為編輯工具準(zhǔn)備的,具體來說就是 Android Studio 和布局文件編譯器(AAPT**)雳旅。 在開發(fā)程序的時候间聊,通過這些屬性來告訴編輯器一些額外的信息哎榴。
tools:ignore
這個屬性是為** Lint 準(zhǔn)備的,屬性的取值為逗號分隔的 Lint 問題 ID**尚蝌。告訴 Lint 在檢測代碼的時候驼壶,這些問題不用檢測喉酌。例如:
<string name=”show_all_apps” tools:ignore=”MissingTranslation”>All</string>
tools:targetApi
這個屬性也是為 Lint 準(zhǔn)備的。和 Java 注解 @TargetApi 一樣般妙。取值可以為 Android 版本代號或者對應(yīng)的整數(shù)值相速。例如:
<GridLayout tools:targetApi=”ICE_CREAM_SANDWICH” >
tools:locale
Lint 和 Android Studio 都會使用該屬性。取值為 語言和地區(qū)縮寫苫拍。如果設(shè)置該值為非英文類型,則 Studio 不會執(zhí)行拼寫檢查骏令。該屬性通常出現(xiàn)在資源文件的根元素上垄提。用來告訴 Lint 該文件應(yīng)該用在那種語言環(huán)境中铡俐。 例如:values/strings.xml 文件 設(shè)置該屬性為 es
<resources xmlns:tools=”http://schemas.android.com/tools” tools:locale=”es”>
上面的設(shè)置告訴工具,默認(rèn)的文件夾中的資源為西班牙語而不是英語吏够。
tools:context
Lint 和 Android Studio 都會使用該屬性稿饰。出現(xiàn)在布局根元素中露泊,用來告訴工具該布局文件和哪個 Activity 關(guān)聯(lián)。這樣在設(shè)計的時候侣姆,布局編輯器會使用相關(guān)聯(lián)的 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> 等。這樣在設(shè)計的時候标锄,編輯器可以用指定的內(nèi)容來顯示預(yù)覽內(nèi)容茁计。
<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 上的菜單內(nèi)容匕争。取值為定義的 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 上的導(dǎo)航模式屑迂。取值為 “standard”惹盼、 “l(fā)ist” 和”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” />
除了上面這些屬性以外,標(biāo)準(zhǔn)的 Android 布局屬性都可以當(dāng)做 tools 屬性來使用掩蛤。布局編輯器用這些屬性來選擇布局文件枉昏。這些屬性被稱之為 “設(shè)計時布局屬性”,他們只被布局編輯器使用揍鸟,編譯后的代碼中不存在這些內(nèi)容兄裂。
例如:常用于即時預(yù)覽填充 tools:text="weixiong_wang"等(最常用,最好用)阳藻。
參考:
http://blog.chengyunfeng.com/?p=755&utm_source=tuicool&utm_medium=referral