標籤:多個 其他 問題 技術分享 ast 需要 提高 tap textview
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 屬性來使用。布局編輯器用這些屬性來選擇布局檔案。這些屬性被稱之為 “ 設計時布局屬性 ”,他們只被布局編輯器使用,編譯後的代碼中不存在這些內容。
例如
<RelativeLayoutxmlns: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屬性上:<EditTextandroid: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
Android 開發小工具之:Tools 屬性 (轉)