開發(fā)人員在設(shè)計(jì)Android Layout布局時(shí),總會(huì)伴隨著一些亂七八槽的困擾拔创。比如利诺,為了更加逼真的真實(shí)數(shù)據(jù)預(yù)覽效果,我們?cè)陂_發(fā)時(shí)會(huì)將TextView的text屬性寫上一些假數(shù)據(jù)剩燥,而當(dāng)運(yùn)行到模擬器或真機(jī)上時(shí)這些假數(shù)據(jù)就成了造成體驗(yàn)上甚至測(cè)試BUG的臟數(shù)據(jù)慢逾,又需要一一清除。再比如灭红,我們想在XML的預(yù)覽界面上就看到ListView的Item內(nèi)容侣滩,而不是只有通過(guò)編譯運(yùn)行時(shí)才能查看。等等变擒,諸如這些存在于開發(fā)Layout內(nèi)容階段的困擾君珠,都可以通過(guò)Tools Attributes得以解決,不妨了解一下娇斑。
xmlns 定義
Android提供了一種特殊的tools命名空間策添,來(lái)幫助開發(fā)人員在開發(fā)Layout布局時(shí)使用tools屬性解決實(shí)時(shí)預(yù)覽等問(wèn)題材部。這種屬性在應(yīng)用打包運(yùn)行時(shí)將會(huì)被忽略,不產(chǎn)生任何影響唯竹。tools命名空間的URI為:http://schemas.android.com/tools
乐导,通常可將其定義在Layout的根容器中(在Android Studio中浸颓,可利用Live Temples快捷方式物臂,輸入toolsNs即可對(duì)應(yīng)提示),如:
<?xml version="1.0" encoding="utf-8"?>
<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">
</RelativeLayout>
這里將Tools Attributes按照功能分為兩種類別产上,一種是去除Lint提示的鹦聪,一種是展示布局預(yù)覽的,下面一一介紹相關(guān)屬性的使用蒂秘。
Lint 提示
tools:ignore
這個(gè)屬性用于告訴Android Lint忽視某些xml警告,比如平時(shí)使用ImageView的時(shí)候都要加上這么一句:android:contentDescription="@null"
淘太,否則便會(huì)出現(xiàn)黃色警告:[Accessibility] Missing contentDescription attribute on image姻僧,此時(shí)就可以使用tools:ignore
屬性忽視這個(gè)警告:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="contentDescription"/>
tools:targetApi
類似Java代碼中的@TargetApi
注解一樣,指定API級(jí)別蒲牧,可以使用數(shù)字level撇贺,也可以直接用API name,比如elevation屬性只能在API 21及更高版本使用冰抢,使用tools:targetApi
屬性可以忽視版本的警告:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:targetApi="LOLLIPOP"/>
tools:locale
指定IDE當(dāng)前的假定本地語(yǔ)言松嘶,可用在資源文件的根元素上,比如用在values/strings.xml
上挎扰,避免一些拼寫語(yǔ)法上的檢查:
<resources xmlns:tools="http://schemas.android.com/tools"
tools:local="es">
<!-- string -->
<string name="name">nombre</string>
</resources>
布局預(yù)覽
這里也分為兩種翠订,一種是替換標(biāo)準(zhǔn)的android命名空間的控件固有屬性,舉個(gè)例子:
<TextView
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Samples"/>
上例中使用tools:text替換了android:text標(biāo)準(zhǔn)屬性遵倦,在design窗口預(yù)覽時(shí)可以看到TextView的text內(nèi)容尽超,而運(yùn)行時(shí)則會(huì)被忽略。諸如此類梧躺,其他android標(biāo)準(zhǔn)屬性均可被替換以達(dá)到預(yù)覽效果似谁。另一種就是非android標(biāo)準(zhǔn)屬性了,下面一一說(shuō)明掠哥。
tools:context
這個(gè)屬性用在layout文件的根元素上巩踏,指明與當(dāng)前l(fā)ayout相關(guān)聯(lián)的Activity,從而在預(yù)覽時(shí)使用Activity的主題(theme一般定義在Manifest文件中并且與activities聯(lián)系在一起续搀,而非layout)塞琼。可以使用Activity的全名目代,也可以利用manifest中定義的包名作為前綴:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
tools:layout
這個(gè)屬性主要用于<fragment>標(biāo)簽中屈梁,指定預(yù)覽時(shí)用的layout布局文件內(nèi)容:
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_content"/>
tools:listitem/listheader/listfooter
顧名思義嗤练,這三個(gè)屬性可用于諸如ListView、GridView在讶、ExpandableListView等AdapterView的子類中煞抬,實(shí)現(xiàn)內(nèi)容布局的預(yù)覽。注意:經(jīng)實(shí)踐构哺,在Android Studio中已經(jīng)無(wú)法達(dá)到這些元素的內(nèi)容預(yù)覽效果革答,但tools:listitem
屬性可以用在RecyclerView
中,比如:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@android:layout/simple_list_item_checked"/>
RecyclerView使用tools:listitem的前后對(duì)比預(yù)覽效果如下:
tools:showIn
這個(gè)屬性用在<include>標(biāo)簽所包含的layout的根元素上曙强,指定一個(gè)<include>所在的布局文件残拐,這樣在被嵌套的layout的design視圖中便可以預(yù)覽到外層的layout內(nèi)容。比如在activity_main.xml中使用<include>
標(biāo)簽嵌套一個(gè)include_content.xml文件碟嘴,那么在include_content.xml文件的根元素中就可以使用tools:showIn
屬性指定Outer layout溪食,達(dá)到在include_content.xml文件中預(yù)覽activity_main.xml內(nèi)容的效果:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
tools:menu
這個(gè)屬性用在layout根元素中,指定layout預(yù)覽時(shí)ActionBar展示的menu內(nèi)容娜扇。使用tools:context
屬性時(shí)错沃,ActionBar會(huì)自動(dòng)查找Activity中的onCreateOptionsMenu()
方法預(yù)覽menu,tools:menu
屬性的設(shè)置會(huì)覆蓋tools:context
屬性的效果雀瓢。tools:menu
的值只需要使用menu文件的名字即可枢析,多個(gè)menu使用逗號(hào)隔開,如
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:menu="menu1,menu2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
tools:actionBarNavMode
這個(gè)屬性用在layout根元素中刃麸,指定layout預(yù)覽時(shí)ActonBar的導(dǎo)航模式醒叁,值有standard、list和tabs三種泊业,如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:actionBarNavMode="standard"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
ActionBar不同navigation mode下的預(yù)覽效果如圖所示把沼,關(guān)于navigation mode的相關(guān)信息可參考Top Level Switching With View Controls:
tools:shrinkMode/keep/discard
為了使APK文件盡可能地變小,一般在打包發(fā)布時(shí)會(huì)開啟Shrink Code和Shrink Resources的功能脱吱,刪除項(xiàng)目中無(wú)用的代碼和資源智政,使用這三個(gè)屬性可以指定某些resources的保留與刪除,具體信息可以參考官網(wǎng)介紹:Shrink Your Code and Resources箱蝠。
官方參考
以上便是筆者對(duì)于Android Tools Attributes的一些理解和實(shí)踐续捂,英文較好的也可以直接參考官方介紹,地址如下: