Android Studio tools attributes

Android Studio支持tools命名空間中的各種xml屬性,這些屬性支持設計時功能和編譯時行爲。當構建應用時,編譯工具會移除這些屬性,因此它們不會對APK大小或運行時行爲產生影響。

要使用這些屬性,需要將tools命名空間添加到您要在其中使用他們的各個 XML 文件的根元素中,如下所示:

<RootTag xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

PS:直接打出tools,然後使用自動提示功能可以自動添加tools命名空間。

錯誤處理屬性


以下屬性可以幫助抑制Lint警告信息。

tools:ignore
適用於:任何元素
使用者:Lint

此屬性用於接受您希望工具針對相應元素或它的任何子元素忽略的 Lint 問題 ID 的逗號分隔列表。

例如,您可以告知工具忽略 MissingTranslation 錯誤:

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

tools:targetApi
適用於:任何元素
使用者:Lint

此屬性的工作方式與 Java 代碼中的 @TargetApi 註解相同:通過它,您可以指定支持相應元素的 API 級別(指定爲整數或代號)。

它會告知工具您認爲該元素(及任何子元素)將只能在指定的 API 級別或更高級別中使用。這樣,如果該元素或其屬性在您指定爲 minSdkVersion 的 API 級別中不可用,Lint 將不會向您發出警告。

例如,GridLayout 只能在 API 級別 14 及更高級別中使用,但您知道此佈局不會用於任何更低版本,在這種情況下,您就可以使用此屬性:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:targetApi="14" >

tools:locale
適用於:<resources>
使用者:Lint、Android Studio 編輯器

此屬性用於告知工具用於給定 <resources> 元素中各項資源的默認語言/語言區域(因爲工具會假設爲英語),以免拼寫檢查工具發出警告。值必須是有效的語言區域限定符

例如,可以將此屬性添加到 values/strings.xml 文件(默認字符串值)中,以指明用於默認字符串的語言。

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

設計時視圖屬性

以下屬性定義了僅會在 Android Studio 佈局預覽中看到的佈局特性。

tools: 代替 android:
適用於:<View>
使用者:Android Studio 佈局編輯器

可以通過將 tools: 前綴(而不是 android:)與 Android 框架中的任意 <View> 屬性搭配使用,在佈局預覽中插入示例數據。此屬性在以下情況下很有用:在運行時之前,屬性的值不會填充,但您希望提前在佈局預覽中看到效果。

例如,如果 android:text 屬性值在運行時設置,或您希望在佈局中看到默認值以外的值,則可以添加 tools:text,以便指定一些僅在佈局預覽中顯示的文本。

也可以同時添加 android: 命名空間屬性(在運行時使用)和匹配的 tools: 屬性(會替換僅在佈局預覽中顯示的運行時屬性)。

還可以使用 tools: 屬性來取消將某屬性設置爲僅用於佈局預覽。例如,如果擁有的 FrameLayout 包含多個子元素,但希望僅在佈局預覽中看到一個子元素,則可以將其中一個子元素設置爲在佈局預覽中不可見,如下所示:

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second"
        tools:visibility="invisible"  />
    

tools:context
適用於:任何根 <View>
使用者:Lint、Android Studio 佈局編輯器

此屬性用於聲明相應佈局默認與哪個 Activity 相關聯。這會在編輯器或佈局預覽中啓用具有以下特徵的功能:需要了解相應 Activity 的信息,例如預覽中的佈局主題應該是什麼,以及通過快速修復創建 onClick 處理程序時將其插入到什麼位置。

可以使用清單文件中所用的同一點前綴指定 Activity 類名稱(不包括完整的包名稱)。

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity" >

tools:itemCount
適用於:<RecyclerView>
使用者:Android Studio 佈局編輯器

對於給定的 RecyclerView,此屬性會指定佈局編輯器應該在 Preview 窗口中呈現的內容數量。

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:itemCount="3"/>
  

tools:layout
適用於:<fragment>
使用者:Android Studio 佈局編輯器

此屬性用於聲明您希望佈局預覽在 Fragment 內繪製的佈局(因爲佈局預覽無法執行正常情況下會應用佈局的 Activity 代碼)。

<fragment android:name="com.example.master.ItemListFragment"
        tools:layout="@layout/list_content" />

tools:listitem / tools:listheader / tools:listfooter
適用於:<AdapterView>(以及 <ListView> 等子類)
使用者:Android Studio 佈局編輯器

這些屬性用於指定要在列表的項目、標題和頁腳佈局預覽中顯示的佈局。佈局中的任何數據字段都填充有數字內容(例如“Item 1”),以確保列表的項目不會重複。

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/sample_list_item"
        tools:listheader="@layout/sample_list_header"
        tools:listfooter="@layout/sample_list_footer" />
    

tools:showIn
適用於:<include> 引用的佈局中的任何根 <View>
使用者:Android Studio 佈局編輯器

通過此屬性,您可以指向將相應佈局用作內含佈局的佈局,以便在它出現並嵌入到其父級佈局中時預覽(和修改)此文件。

<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" />
    

現在當此 TextView 佈局出現在 activity_main 佈局內部時,佈局預覽中會進行顯示。

tools:menu
適用於:任何根 <View>
使用者:Android Studio 佈局編輯器

此屬性用於指定佈局預覽應該在應用欄中顯示的菜單。值可以是一個或多個菜單 ID,以英文逗號分隔(不帶 @menu/ 或任何此類 ID 前綴,且不帶 .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:minValue / tools:maxValue
適用於:<NumberPicker>
使用者:Android Studio 佈局編輯器

這些屬性用於設置 NumberPicker 視圖的最小值和最大值。

<NumberPicker xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/numberPicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:minValue="0"
        tools:maxValue="10" />

tools:openDrawer
適用於:<DrawerLayout>
使用者:Android Studio 佈局編輯器

通過此屬性,您可以在佈局編輯器的 Preview 窗格中打開 DrawerLayout

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start" />

"@tools:sample/*" 資源
適用於:支持界面文本或圖片的任何視圖。
使用者:Android Studio 佈局編輯器

通過此屬性,您可以將佔位符數據或圖片注入到視圖中。例如,如果需要測試佈局在採用文本時的行爲,但尚未最終確定應用的界面文本,則可以使用佔位符文本,如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="@tools:sample/lorem" />

資源壓縮屬性

通過以下屬性,您可以啓用嚴格引用檢查,並在使用資源壓縮時聲明保留還是捨棄某些資源。

要啓用資源壓縮,請在 build.gradle 文件中將 shrinkResources 屬性(以及適用於代碼壓縮的 minifyEnabled)設置爲 true。例如:

android {
        ...
        buildTypes {
            release {
                shrinkResources true
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'),
                        'proguard-rules.pro'
            }
        }
    }
    

 tools:shrinkMode
適用於:<resources>
使用者:具有資源壓縮功能的編譯工具

通過此屬性,您可以指定編譯工具應該使用“安全模式”(謹慎行事,保留所有明確引用的資源,以及可以通過調用 Resources.getIdentifier() 動態引用的資源)還是“嚴格模式”(僅保留代碼或其他資源中明確引用的資源)。

默認使用“安全模式”(shrinkMode="safe")。要改爲使用“嚴格模式”,請將 shrinkMode="strict" 添加到 <resources> 標記,如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:tools="http://schemas.android.com/tools"
        tools:shrinkMode="strict" />
    

啓用“嚴格模式”後,您可能需要使用 tools:keep 來保留已移除但您實際上需要的資源,並使用 tools:discard 來明確移除更多資源。

如需瞭解詳情,請參閱壓縮資源

tools:discard
適用於:<resources>
使用者:具有資源壓縮功能的編譯工具

使用資源壓縮功能刪除不使用的資源時,您可以通過此屬性指定要手動捨棄的資源(通常情況下,原因是:相應資源被引用,但引用方式不會影響您的應用,或者 Gradle 插件錯誤地推導出相應資源被引用)。

要使用,請在資源目錄中創建一個具有 <resources> 標記的 XML 文件(例如,在 res/raw/keep.xml 處),並將要保留在 tools:discard 屬性中的各項資源指定爲逗號分隔列表。您可以將星號字符用作通配符。例如:

<?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:tools="http://schemas.android.com/tools"
        tools:discard="@layout/unused_1" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章