Android 開發小工具之:Tools 屬性
http://blog.chengyunfeng.com/?p=755#ixzz4apLZhfmi
今天來介紹一些 Android 開發過程中比較有用但是大家又不常用的小工具。這些小工具可以提高 Android 應用開發的效率、還可以提高代碼質量。所以還是有必要使用的。
首先介紹布局文件中的 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” … >
tools:layout
該屬性由 Android Studio 使用。通常用在 <fragment> 元素中,用來告訴編輯器該 fragment 使用的是哪個布局文件。
<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 屬性來使用。布局編輯器用這些屬性來選擇布局文件。這些屬性被稱之為 “ 設計時布局屬性 ”,他們只被布局編輯器使用,編譯后的代碼中不存在這些內容。
例如
<RelativeLayout
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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" tools:text="hello world" android:layout_width="wrap_content" tools:layout_width="400dp" tools:background="@android:color/holo_red_dark" android:layout_height="wrap_content"/> </RelativeLayout> 上面的布局文件,在布局編輯器中預覽圖如下:
這樣做的好處是,在編輯布局文件的時候,不用為了預覽效果而給真正的屬性設置一些測試的值,然后當程序發布的時候, 這些測試的內容忘記刪除了。例如 為了預覽 EditText 的顯示效果,你把一段文字設置到 text屬性上: <EditText android:text="John Doe" android:layout_width="wrap_content" android:layout_height="wrap_content" />
當你發布項目的時候,卻忘記了把該字符串給刪除了。 如果這里面包含有比較機密的內容的話,你的秘密就被泄露出去了。 你可以用
tools:text="John Doe"
這樣在發布的時候,這些內容會自動去掉。
設計時布局屬性的限制:
- 目前只支持標准的屬性,也就是依 android 命名空間下的屬性。
- 目前代碼提示支持的還不是很好,建議先用 android 命名空間來編輯該屬性,然后把 android 替換為 tools。
- 設計時布局屬性只能在布局文件中使用,不能在其他資源文件中使用。
- 這類還有一些問題需要注意: https://code.google.com/p/android/issues/detail?id=46186
