參考:
1.Android:Tools命名空間原來是有大用處的
2.Android中tools屬性的使用
3.工具屬性參考文檔
4. 命名空間介紹
5. 注解
6. lint
7. 資源壓縮shrink-resources
目錄
- 一、概述
- 二、引入tools命名空間
- 三、tools 命名空間的作用有哪些?
- 四、tools 命名空間屬性功能詳解
- (一)、xml 中的錯誤處理屬性
- 1、tools:ignore
- 2、tools:targetApi
- 3、 tools:locale
- (二)、xml視圖預覽相關屬性
- 1、用 tools:xxxx 替代 android:xxxx
- 2、tools:context
- 3、tools:itemCount
- 4、tools:layout
- 5、tools:listitem 、 tools:listheader 、 tools:listfooter
- 6、 tools:showIn
- 7、 tools:menu
- 8、 tools:minValue / tools:maxValue
- 9、 tools:openDrawer
- 10、 "@tools:sample/*" 資源
- (三)、資源壓縮相關屬性 ([Resource shrinking](https://developer.android.google.cn/studio/build/shrink-code?hl=zh-cn#shrink-resources) attributes)
- 1、 tools:shrinkMode
- 2、 tools:keep
- 3、 tools:discard
一、概述
??Android Studio在tools命名空間中支持一些XML屬性來開關設計功能和編譯時行為。當構建應用時,構建工具會刪除這些屬性,從而不會影響APK的大小和運行行為。
二、引入tools命名空間
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" >
三、tools 命名空間的作用有哪些?
根據官方文檔描述,根據其屬性的功能類別,大致有三種主要功能:
- xml中的錯誤處理
- xml 預覽
- 資源壓縮
說的通俗一點就是:
- 減少或者避免黃線提示,讓代碼更清爽,讓編譯少報錯
- 讓預覽界面更靈活,可以隨心所欲的定制預覽視圖
- 壓縮資源文件,降低APK體積。
四、tools 命名空間屬性功能詳解
(一)、xml 中的錯誤處理屬性
1、tools:ignore
說明 | |
---|---|
應用范圍 | xml中的任意元素 |
作用對象 | Lint (Lint 是AndroidStudio提供的代碼掃描工具) |
具體作用 | 讓Lint 工具在檢查代碼時忽略指定的錯誤。 |
取值說明 | 不同的錯誤對應不同的id,這些id 就是 ignore的取值。如:MissingTranslation。ignore后面可以同時跟多個id,多個id之間使用逗號分割 |
示例1:
??Lint 檢查時默認語言為 英文,如果在 xml 中有中文,就會報 MissingTranslation 錯誤,我們加上 tools:ignore 之后即可避免。
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>
示例2:
2、tools:targetApi
說明 | |
---|---|
應用范圍 | xml的任意元素 |
作用對象 | Lint |
具體作用 | 同 java 代碼中的 @TargetApi 注解, 指明某個控件只在指定的API 及更高的版本中生效。這樣,在使用 Lint 檢測時就不會因 minSdkVersion 低于控件出現的版本而報錯。 |
取值說明 | API 版本號對應的 int值 |
示例:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"tools:targetApi="14" >
3、 tools:locale
說明 | |
---|---|
應用范圍 | <resources> |
作用對象 | Lint, Android Studio editor |
具體作用 | 指明 resources 中元素的語言類型,避免拼寫檢查或者Lint 檢查時報錯。這兩者中默認的語言類型時英文 es |
取值說明 |
示例:
??我們在 values/strings.xml
中指明元素的語言版本。
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">
(二)、xml視圖預覽相關屬性
以下屬性在xml中定義之后,只在預覽時會展示,正式部署之后并不會展示。類似于 DataBindg 中引用字符串資源時的 default 屬性。
1、用 tools:xxxx 替代 android:xxxx
說明 | |
---|---|
應用范圍 | view |
作用對象 | Android Studio布局編輯器 |
具體作用 | 將view的任意屬性值的 android 前綴替換為 tools 之后,就可以實現預覽效果。以tools 為命名空間的屬性值只在預覽時有效。 另外,在預覽時,如果同時有 tools:xxx 和 android:xxx ,則優先展示 tools:xxx 的預覽效果, 可參考示例代碼2 |
取值說明 | 具體取值以view的屬性取值為準 |
示例代碼1:
??預覽時展示指定文本
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"tools:text="歡迎關注 CnPeng 公眾號"/>
示例代碼2:
??tools:text 和 android:text 同時存在,在預覽時會優先展示 tools:text
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"tools:text="這些在預覽時展示,并會在預覽時優先于 android:text 展示"android:text="這些在部署之后會展示"/>
2、tools:context
說明 | |
---|---|
應用范圍 | xml 中的根布局 |
作用對象 | Lint, Android Studio布局編輯器 |
具體作用 | **聲明該布局文件默認關聯的 activity。**聲明之后會在布局編輯器或者預覽界面中開啟一些與該activity相關的特性,比如,在寫 onClick時,直接輸入方法名,然后點擊自動完成代碼的快捷鍵就會提示你在對應activity中創建該方法。This enables features in the editor or layout preview that require knowledge of the activity, such as what the layout theme should be in the preview and where to insertonClickhandlers when you make those from a quickfix . |
取值說明 | 關聯的activity。需要帶路徑,建議與清單文件中注冊 activity時的路徑保持一致。 |
示例代碼:
??先聲明關聯的activity,然后直接寫 onclick 方法名,然后按下自動完成代碼的快捷鍵,就會提示在對應的activity中創建該方法。
3、tools:itemCount
說明 | |
---|---|
應用范圍 | <RecyclerView> |
作用對象 | Android Studio 布局編輯器 |
具體作用 | 在 <RecyclerView> 節點中設置該屬性之后,會指定在預覽界面中繪制/展示幾個條目 |
取值說明 | int 類型數值 |
示例代碼:
??預覽界面展示 4個 條目
4、tools:layout
說明 | |
---|---|
應用范圍 | < fragment> |
作用對象 | Android Studio 布局編輯器 |
具體作用 | 聲明在預覽時將哪個布局文件填充到該Fragment |
取值說明 | 布局id 的引用值 |
示例代碼:
??在預覽時將 testlayout 這個布局文件填充到fragment。testlayout的布局中包含一個 RecyclerView,并通過 itemCount 設置的預覽時展示的條數為4(參考 tools:itemCount)
5、tools:listitem 、 tools:listheader 、 tools:listfooter
說明 | |
---|---|
應用范圍 | <AdapterView>及其子類,如<ListView> |
作用對象 | Android Studio 布局編輯器 |
具體作用 | 指明 AdapterView在預覽界面中所展示的 條目、頭布局、腳步局 |
取值說明 | 布局文件的引用 |
示例代碼:
item_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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:gravity="center_vertical"android:orientation="horizontal"><ImageViewandroid:layout_width="@dimen/dp50"android:layout_height="@dimen/dp50"android:src="@drawable/logo"tools:ignore="ContentDescription"/><!--ignore 后面根由多個錯誤id時,用逗號分隔 ; --><TextViewandroid:id="@+id/tv_item_suspendRv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="@dimen/dp10"android:gravity="center_vertical"android:text="abc"tools:ignore="HardcodedText,RtlHardcoded"/><!--使用 tools:text 設置預覽文本--><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"tools:text="這是設置的預覽文本"/>
</LinearLayout>
注意
如果 條目布局中有 TextView及其子類控件
(1) 如果設置了 tools:text , 在預覽時會優先展示該值;
(2) 如果沒有設置 tools:text ,但設置了 android:text , 在預覽時就會展示android:text 的屬性值;
(3) 如果都沒有設置,則會默認使用 item1、item2 填充到 TextView中作為預覽文本
6、 tools:showIn
說明 | |
---|---|
應用范圍 | 所有 <view> 的根節點(即 布局文件的根節點) |
作用對象 | Android Studio 布局編輯器 |
具體作用 | 聲明該布局文件將會被哪個布局通過 <include>引用。聲明之后,在對應的文件中不要忘了用 <include>引用 |
取值說明 | 布局文件的引用 |
示例代碼:
??testlayout2.xml 將會被 testlayout 引用。
testlayout2.xml
<?xml version="1.0" encoding="utf-8"?>
<TextViewandroid:id="@+id/testFragment"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"tools:showIn="@layout/testlayout"tools:text="預覽文本"></TextView>
testlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><include layout="@layout/testlayout2"/></LinearLayout>
通過這種方式我們在確認該布局文件在哪里使用了的時候就比較方便了。
7、 tools:menu
說明 | |
---|---|
應用范圍 | 布局文件的根節點(Any root <View> ) |
作用對象 | Android Studio 布局編輯器 |
具體作用 | 聲明在預覽界面中 AppBar 將展示哪些菜單 |
取值說明 | menu文件的id,多個id 之間用逗號間隔。不需要任何前綴和后綴。The value can be one or more menu IDs, separated by commas (without @menu/ or any such ID prefix and without the .xml extension) |
注意:按照官方文檔的說明,可以傳入多個 menu id 。但是實際測試時發現,傳入多個時右上角并沒有什么不同的顯示。
8、 tools:minValue / tools:maxValue
說明 | |
---|---|
應用范圍 | <NumberPicker> |
作用對象 | Android Studio 布局編輯器 |
具體作用 | 為 NumberPicker 設置預覽時的最小值和最大值 |
取值說明 | int 型數值 |
示例說明:
9、 tools:openDrawer
說明 | |
---|---|
應用范圍 | <DrawerLayout> |
作用對象 | Android Studio 布局編輯器 |
具體作用 | 在預覽界面中將 DrawerLayout 打開。 |
取值說明 | end、left、right、start。具體說明,參考下表 |
Constant | Value | Description |
---|---|---|
end | 800005 | Push object to the end of its container, not changing its size. |
left | 3 | Push object to the left of its container, not changing its size. |
right | 5 | Push object to the right of its container, not changing its size. |
start | 800003 | Push object to the beginning of its container, not changing its size. |
注意:
1、在 <DrawerLayout > 需要通過 layout_gravity 聲明哪一部分作為側拉窗口,其取值也是 end、start、left、right。
2、tools:openDrawer 的取值必須與側拉窗口的 layout_gravity 的取值一致
<?xml version="1.0" encoding="utf-8"?><!--此處 openDrawer 的取值必須與側拉窗口的 layout_gravity 取值一致-->
<android.support.v4.widget.DrawerLayoutandroid:id="@+id/numberPicker"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"tools:openDrawer="right"><!--這是未展示側拉界面時的主體內容--><RelativeLayout android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"tools:text="這是主內容"/></RelativeLayout><!--這是側拉窗口中的內容。必須通過 layout_gravity 屬性聲明這是一個側拉展示的內容--><RelativeLayoutandroid:layout_width="100dp"android:layout_height="match_parent"android:layout_gravity="right"android:background="#f2e67b"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:ems="1"tools:text="這是要側拉的東西"/></RelativeLayout>
</android.support.v4.widget.DrawerLayout>
10、 “@tools:sample/*” 資源
說明 | |
---|---|
應用范圍 | 所有支持顯示 text 或者 image 的view控件(Any view that supports UI text or images) |
作用對象 | Android Studio 布局編輯器 |
具體作用 | 為View設置占位文本或圖片。這其實就是系統預置的一堆字符串和圖片資源,當你想設置預覽文本或者預覽圖片時,如果不想自己去定義,直接引用這些系統預置的字符串和圖片就可以了 |
取值說明 | 參考下表 |
屬性值 | 說明 |
---|---|
@tools:sample/full_names | Full names that are randomly generated from the combination of @tools:sample/first_names and@tools:sample/last_names. |
@tools:sample/first_names | Common first names. |
@tools:sample/last_names | Common last names. |
@tools:sample/cities | Names of cities from across the world. |
@tools:sample/us_zipcodes | Randomly generated US zipcodes. |
@tools:sample/us_phones | Randomly generated phone numbers with the following format: (800) 555-xxxx . |
@tools:sample/lorem | Placeholder text that is derived from Latin. |
@tools:sample/date/day_of_week | Randomized dates and times for the specified format. |
@tools:sample/date/ddmmyy | |
@tools:sample/date/mmddyy | |
@tools:sample/date/hhmm | |
@tools:sample/date/hhmmss | |
@tools:sample/avatars | Vector drawables that you can use as profile avatars. |
@tools:sample/backgrounds/scenic | Images that you can use as backgrounds. |
示例代碼:
在下面的預覽圖中,圖標和文本都是直接引用的系統預置的。
(三)、資源壓縮相關屬性 (Resource shrinking attributes)
??下面這些屬性,可以讓我們在 資源壓縮時確定哪些資源可以保留或者丟棄,也可以讓我們開啟嚴格模式的資源引用檢查。 The following attributes allow you to enable strict reference checks and declare whether to keep or discard certain resources when using resource shrinking
開啟資源壓縮時,在 module 的build.gradle 文件作如下修改:
android {buildTypes {release {shrinkResources true //開啟資源壓縮。minifyEnabled 也必須為true,否則編譯不通過minifyEnabled true //開啟代碼混淆/壓縮proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}
}
1、 tools:shrinkMode
說明 | |
---|---|
應用范圍 | <resources> |
作用對象 | 開啟了資源壓縮的構建工具Build tools with resource shrinking |
具體作用 | 指明 構建工具在壓縮資源時使用哪種模式:safe mode 、strict mode |
取值說明 | safe、strict |
模式 | 說明 |
---|---|
safe | 保留被顯示引用的,或者可能通過 Resources.getIdentifier() 被動態引用的資源 |
strict | 保留 resources 或者 代碼中 被顯示引用的資源 |
??默認是 safe 模式 (即shrinkMode="safe"
). 如果想使用 strict 模式,需要在<resources>節點中顯示聲明 shrinkMode="strict"
,具體如下:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:shrinkMode="strict" />
??開啟 strict 模式之后, 可以使用 tools:keep
保留那些你不想被移除的資源, 或者使用tools:discard
直接移除資源
2、 tools:keep
說明 | |
---|---|
應用范圍 | <resources> |
作用對象 | 開啟了資源壓縮的構建工具 |
具體作用 | 使用資源壓縮去移除未被使用的資源時,該屬性將允許你指明哪些資源可以被保留(比如一些通過Resources.getIdentifier() 間接引用的資源) |
取值說明 | 資源文件的引用 |
??使用時,在 resources 目錄下創建一個 xml 文件并指定名稱,如:res/raw/keep.xml
。創建一個<resources>
節點,并為 tools:keep
賦值,其值代表將被保留的資源,多個資源之間使用逗號間隔,也可以使用 * 作為通配符,示例如下:
<?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" />
3、 tools:discard
說明 | |
---|---|
應用范圍 | <resources> |
作用對象 | 開啟了資源壓縮的構建工具 |
具體作用 | |
取值說明 | |
??當使用資源壓縮工具去除一些無用資源時,使用該屬性可以指明一些需要手動刪除的資源 (比如:被引用了但是未能生效的資源,或者 Gradle 插件誤引用了某些資源被引用). |
??使用時,在 resources 目錄下創建一個 xml 文件并指定名稱,如:res/raw/keep.xml
。創建一個<resources>
節點,并為 tools:keep
賦值,其值代表將被保留的資源,多個資源之間使用逗號間隔,也可以使用 * 作為通配符,示例如下:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:discard="@layout/unused_1" />