Android Tools Attributes,讓布局設(shè)計(jì)所見即所得

開發(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)容,而不是只有通過編譯運(yùn)行時(shí)才能查看。等等,諸如這些存在于開發(fā)Layout內(nèi)容階段的困擾,都可以通過Tools Attributes得以解決,不妨了解一下。

xmlns 定義


Android提供了一種特殊的tools命名空間,來幫助開發(fā)人員在開發(fā)Layout布局時(shí)使用tools屬性解決實(shí)時(shí)預(yù)覽等問題。這種屬性在應(yīng)用打包運(yùn)行時(shí)將會(huì)被忽略,不產(chǎn)生任何影響。tools命名空間的URI為:http://schemas.android.com/tools,通??蓪⑵涠x在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)前的假定本地語言,可用在資源文件的根元素上,比如用在values/strings.xml上,避免一些拼寫語法上的檢查:

<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)屬性了,下面一一說明。

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)無法達(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-listitem

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

navigation mode

tools:shrinkMode/keep/discard

為了使APK文件盡可能地變小,一般在打包發(fā)布時(shí)會(huì)開啟Shrink Code和Shrink Resources的功能,刪除項(xiàng)目中無用的代碼和資源,使用這三個(gè)屬性可以指定某些resources的保留與刪除,具體信息可以參考官網(wǎng)介紹:Shrink Your Code and Resources。

官方參考


以上便是筆者對(duì)于Android Tools Attributes的一些理解和實(shí)踐,英文較好的也可以直接參考官方介紹,地址如下:

http://tools.android.com/tech-docs/tools-attributes

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評(píng)論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,384評(píng)論 0 17
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,898評(píng)論 2 45
  • 最近經(jīng)歷的一件事情,又讓我重新認(rèn)識(shí)了“做得到”和“自以為做得到”之間的區(qū)別。 兩周之前,朋友約我?guī)兔Α安饡?,因?yàn)?..
    膽小的鱷魚閱讀 920評(píng)論 2 3
  • 我迷迷糊糊地睜開了眼睛,漸漸地恢復(fù)了意識(shí)。突然腳下一涼,我下意識(shí)地縮了一下腿,這個(gè)動(dòng)作竟然讓我疼得大喊了出來。 我...
    半朽閱讀 731評(píng)論 23 20

友情鏈接更多精彩內(nèi)容