Android Studio 支持 tools
命名空間中的多種 XML 屬性芒粹,這些屬性支持設(shè)計(jì)時(shí)功能(例如要在 Fragment 中顯示哪種布局)或編譯時(shí)行為(例如要對(duì) XML 資源應(yīng)用哪種壓縮模式)。在您構(gòu)建應(yīng)用時(shí)大溜,編譯工具會(huì)移除這些屬性化漆,因此它們不會(huì)對(duì) APK 大小或運(yùn)行時(shí)行為產(chǎn)生影響。
要使用這些屬性钦奋,請(qǐng)將 tools
命名空間添加到您要在其中使用他們的各個(gè) XML 文件的根元素中座云,如下所示:
錯(cuò)誤處理屬性
以下屬性可幫助抑制 Lint 警告消息。
-
tools:ignore
適用于:任何元素
使用者:Lint
此屬性用于接受您希望工具針對(duì)相應(yīng)元素或它的任何子元素忽略的 Lint 問題 ID 的逗號(hào)分隔列表付材。
例如朦拖,您可以告知工具忽略MissingTranslation
錯(cuò)誤:
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>
-
tools:targetApi
適用于:任何元素
使用者:Lint
此屬性的工作方式與 Java 代碼中的@TargetApi
注解相同:通過它,您可以指定支持相應(yīng)元素的 API 級(jí)別(指定為整數(shù)或代號(hào))厌衔。
它會(huì)告知工具您認(rèn)為該元素(及任何子元素)將只能在指定的 API 級(jí)別或更高級(jí)別中使用璧帝。這樣,如果該元素或其屬性在您指定為minSdkVersion
的 API 級(jí)別中不可用富寿,Lint 將不會(huì)向您發(fā)出警告睬隶。
例如,GridLayout
只能在 API 級(jí)別 14 及更高級(jí)別中使用页徐,但您知道此布局不會(huì)用于任何更低版本苏潜,在這種情況下,您就可以使用此屬性:
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:targetApi="14" >
但是泞坦,您應(yīng)該使用支持庫中的 GridLayout
:
-
tools:locale
適用于:<resources>
使用者:Lint窖贤、Android Studio 編輯器
此屬性用于告知工具用于給定<resources>
元素中各項(xiàng)資源的默認(rèn)語言/語言區(qū)域(因?yàn)楣ぞ邥?huì)假設(shè)為英語)砖顷,以免拼寫檢查工具發(fā)出警告贰锁。值必須是有效的語言區(qū)域限定符。
例如滤蝠,您可以將此屬性添加到values/strings.xml
文件(默認(rèn)字符串值)中豌熄,以指明用于默認(rèn)字符串的語言是西班牙語,而不是英語物咳。<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">
設(shè)計(jì)時(shí)視圖屬性
以下屬性定義了僅會(huì)在 Android Studio 布局預(yù)覽中看到的布局特性锣险。
-
tools:
代替android:
適用于:<View>
使用者:Android Studio 布局編輯器
您可以通過將tools:
前綴(而不是android:
)與 Android 框架中的任意<View>
屬性搭配使用,在布局預(yù)覽中插入示例數(shù)據(jù)。此屬性在以下情況下很有用:在運(yùn)行時(shí)之前芯肤,屬性的值不會(huì)填充巷折,但您希望提前在布局預(yù)覽中看到效果。
例如崖咨,如果android:text
屬性值在運(yùn)行時(shí)設(shè)置锻拘,或您希望在布局中看到默認(rèn)值以外的值,則可以添加tools:text
击蹲,以便指定一些僅在布局預(yù)覽中顯示的文本署拟。
**圖 1.** `tools:text` 屬性將“Google Voice”設(shè)置為在布局預(yù)覽中顯示的值
您可以同時(shí)添加android:
命名空間屬性(在運(yùn)行時(shí)使用)和匹配的tools:
屬性(會(huì)替換僅在布局預(yù)覽中顯示的運(yùn)行時(shí)屬性)。
您還可以使用 tools:
屬性來取消將某屬性設(shè)置為僅用于布局預(yù)覽歌豺。例如推穷,如果您擁有的 FrameLayout
包含多個(gè)子元素,但您希望僅在布局預(yù)覽中看到一個(gè)子元素类咧,則可以將其中一個(gè)子元素設(shè)置為在布局預(yù)覽中不可見馒铃,如下所示:
<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"** />
在設(shè)計(jì)視圖中使用布局編輯器時(shí),您還可以通過 Properties 窗口修改部分設(shè)計(jì)時(shí)視圖屬性痕惋。每個(gè)設(shè)計(jì)時(shí)屬性都用屬性名稱旁邊的扳手圖標(biāo)進(jìn)行指示骗露,以便與同名的真實(shí)屬性區(qū)分開。
-
tools:context
</devsite-heading>
適用于:任何根<View>
使用者:Lint血巍、Android Studio 布局編輯器
此屬性用于聲明相應(yīng)布局默認(rèn)與哪個(gè) Activity 相關(guān)聯(lián)萧锉。這會(huì)在編輯器或布局預(yù)覽中啟用具有以下特征的功能:需要了解相應(yīng) Activity 的信息,例如預(yù)覽中的布局主題應(yīng)該是什么述寡,以及通過快速修復(fù)創(chuàng)建onClick
處理程序時(shí)將其插入到什么位置(圖 2)柿隙。
**圖 2.** 只有在您設(shè)置了 `tools:context` 時(shí),`onClick` 屬性的快速修復(fù)功能才會(huì)工作鲫凶。
您可以使用清單文件中所用的同一點(diǎn)前綴指定 Activity 類名稱(不包括完整的軟件包名稱)禀崖。例如:<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity" >
提示:您還可以從布局編輯器工具欄中選擇布局預(yù)覽的主題。
-
tools:itemCount
適用于:<RecyclerView>
使用者:Android Studio 布局編輯器
對(duì)于給定的RecyclerView
螟炫,此屬性會(huì)指定布局編輯器應(yīng)該在 Preview 窗口中呈現(xiàn)的內(nèi)容數(shù)量波附。
例如:<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 布局編輯器
此屬性用于聲明您希望布局預(yù)覽在 Fragment 內(nèi)繪制的布局(因?yàn)椴季诸A(yù)覽無法執(zhí)行正常情況下會(huì)應(yīng)用布局的 Activity 代碼)。
例如:<fragment android:name="com.example.master.ItemListFragment" tools:layout="@layout/list_content" />
-
tools:listitem / tools:listheader / tools:listfooter
適用于:<AdapterView>
(以及<ListView>
等子類)
使用者:Android Studio 布局編輯器
這些屬性用于指定要在列表的項(xiàng)目昼钻、標(biāo)題和頁腳布局預(yù)覽中顯示的布局掸屡。布局中的任何數(shù)據(jù)字段都填充有數(shù)字內(nèi)容(例如“Item 1”),以確保列表的項(xiàng)目不會(huì)重復(fù)然评。
例如:<!-----> <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" />
注意:這些屬性不適用于 Android Studio 2.2 中的
ListView
仅财,不過在 Android Studio 2.3 中,該問題(問題 215172)已修復(fù)碗淌。 -
tools:showIn
適用于:<include>
引用的布局中的任何根<View>
使用者:Android Studio 布局編輯器
通過此屬性盏求,您可以指向將相應(yīng)布局用作內(nèi)含布局的布局抖锥,以便在它出現(xiàn)并嵌入到其父級(jí)布局中時(shí)預(yù)覽(和修改)此文件。
例如:<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" />
現(xiàn)在當(dāng)此 TextView
布局出現(xiàn)在 activity_main
布局內(nèi)部時(shí)碎罚,布局預(yù)覽中會(huì)進(jìn)行顯示磅废。
-
tools:menu
適用于:任何根 <View>
使用者:Android Studio 布局編輯器
此屬性用于指定布局預(yù)覽應(yīng)該在應(yīng)用欄中顯示的菜單。值可以是一個(gè)或多個(gè)菜單 ID荆烈,以英文逗號(hào)分隔(不帶 @menu/
或任何此類 ID 前綴还蹲,且不帶 .xml
擴(kuò)展名)。例如:
<?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
</devsite-heading>
適用于:<NumberPicker>
使用者:Android Studio 布局編輯器
這些屬性用于設(shè)置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
</devsite-heading>
適用于:<DrawerLayout>
使用者:Android Studio 布局編輯器
通過此屬性谜喊,您可以在布局編輯器的 Preview 窗格中打開[DrawerLayout](https://developer.android.google.cn/reference/androidx/drawerlayout/widget/DrawerLayout.html)
。您還可以通過傳遞以下任一值修改布局編輯器呈現(xiàn)布局的方式:
| 常量 | 值 | 說明 |
| --- | --- | --- |
| 結(jié)尾 | 800005 | 將對(duì)象推送到其容器的結(jié)尾倦始,不更改其大小斗遏。 |
| 左側(cè) | 3 | 將對(duì)象推送到其容器的左側(cè),不更改其大小鞋邑。 |
| 右側(cè) | 5 | 將對(duì)象推送到其容器的右側(cè)诵次,不更改其大小。 |
| 開頭 | 800003 | 將對(duì)象推送到其容器的開頭枚碗,不更改其大小逾一。 |
例如:<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/*"
資源</devsite-heading>
適用于:支持界面文本或圖片的任何視圖。
使用者:Android Studio 布局編輯器
通過此屬性肮雨,您可以將占位符數(shù)據(jù)或圖片注入到視圖中遵堵。例如,如果您需要測(cè)試布局在采用文本時(shí)的行為怨规,但尚未最終確定應(yīng)用的界面文本陌宿,則可以使用占位符文本,如下所示:<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" />
下表列出了可以注入到布局中的占位符數(shù)據(jù)的類型波丰。
屬性值 占位符數(shù)據(jù)說明 @tools:sample/full_names
從 @tools:sample/first_names
和@tools:sample/last_names
的組合中隨機(jī)生成的全名壳坪。@tools:sample/first_names
常見名字。 @tools:sample/last_names
常見姓氏掰烟。 @tools:sample/cities
世界各地的城市名稱爽蝴。 @tools:sample/us_zipcodes
隨機(jī)生成的美國(guó)郵政編碼。 @tools:sample/us_phones
隨機(jī)生成的電話號(hào)碼纫骑,格式如下: (800) 555-xxxx
蝎亚。@tools:sample/lorem
源自拉丁語的占位符文本。 @tools:sample/date/day_of_week
指定格式的隨機(jī)日期和時(shí)間惧磺。 @tools:sample/date/ddmmyy
@tools:sample/date/mmddyy
@tools:sample/date/hhmm
@tools:sample/date/hhmmss
@tools:sample/avatars
可用作個(gè)人資料頭像的矢量可繪制對(duì)象颖对。 @tools:sample/backgrounds/scenic
可用作背景的圖片。
資源壓縮屬性
通過以下屬性磨隘,您可以啟用嚴(yán)格引用檢查缤底,并在使用資源壓縮時(shí)聲明保留還是舍棄某些資源。
要啟用資源壓縮番捂,請(qǐng)?jiān)?build.gradle
文件中將 shrinkResources
屬性(以及適用于代碼壓縮的 minifyEnabled
)設(shè)置為 true
个唧。例如:
android {
...
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
-
tools:shrinkMode
適用于:<resources>
使用者:具有資源壓縮功能的編譯工具
通過此屬性,您可以指定編譯工具應(yīng)該使用“安全模式”(謹(jǐn)慎行事设预,保留所有明確引用的資源徙歼,以及可以通過調(diào)用Resources.getIdentifier()
動(dòng)態(tài)引用的資源)還是“嚴(yán)格模式”(僅保留代碼或其他資源中明確引用的資源)。默認(rèn)使用“安全模式”(
shrinkMode="safe"
)鳖枕。要改為使用“嚴(yán)格模式”孽鸡,請(qǐng)將shrinkMode="strict"
添加到<resources>
標(biāo)記份乒,如下所示:<?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" tools:shrinkMode="strict" />
啟用“嚴(yán)格模式”后,您可能需要使用
tools:keep
來保留已移除但您實(shí)際上需要的資源,并使用tools:discard
來明確移除更多資源估盘。
如需了解詳情,請(qǐng)參閱壓縮資源怖亭。 -
tools:keep
適用于:<resources>
使用者:具有資源壓縮功能的編譯工具
使用資源壓縮功能移除不使用的資源時(shí)及穗,您可以通過此屬性指定要保留的資源(通常情況下,原因是:相應(yīng)資源在運(yùn)行時(shí)以間接方式被引用哄褒,例如通過將動(dòng)態(tài)生成的資源名稱傳遞給Resources.getIdentifier()
)稀蟋。要使用,請(qǐng)?jiān)谫Y源目錄中創(chuàng)建一個(gè)具有
<resources>
標(biāo)記的 XML 文件(例如呐赡,在res/raw/keep.xml
處)退客,并將要保留在tools:keep
屬性中的各項(xiàng)資源指定為逗號(hào)分隔列表。您可以將星號(hào)字符用作通配符链嘀。例如:<?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@layout/used_1,@layout/used_2,@layout/*_3" />
如需了解詳情井辜,請(qǐng)參閱壓縮資源。
-
tools:discard
適用于:<resources>
使用者:具有資源壓縮功能的編譯工具
使用資源壓縮功能刪除不使用的資源時(shí)管闷,您可以通過此屬性指定要手動(dòng)舍棄的資源(通常情況下粥脚,原因是:相應(yīng)資源被引用,但引用方式不會(huì)影響您的應(yīng)用包个,或者 Gradle 插件錯(cuò)誤地推導(dǎo)出相應(yīng)資源被引用)刷允。
要使用,請(qǐng)?jiān)谫Y源目錄中創(chuàng)建一個(gè)具有<resources>
標(biāo)記的 XML 文件(例如碧囊,在res/raw/keep.xml
處)树灶,并將要保留在tools:discard
屬性中的各項(xiàng)資源指定為逗號(hào)分隔列表。您可以將星號(hào)字符用作通配符糯而。例如:<?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" tools:discard="@layout/unused_1" />
如需了解詳情天通,請(qǐng)參閱壓縮資源。