Android 中xml tools屬性詳解

第一部分

安卓開發(fā)中,在寫布局代碼的時(shí)候,AndroidStudio可以看到布局的預(yù)覽效果椎咧。

但是有些效果則必須在運(yùn)行之后才能看見,比如這種情況:TextView在xml中沒有設(shè)置任何字符把介,而是在activity中設(shè)置了text勤讽。因此為了在AndroidStudio中預(yù)覽效果,你必須在xml中為TextView控件設(shè)置android:text屬性

<TextView
  android:id="@+id/text_main"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="I am a title" />

一般我們在這樣做的時(shí)候都告訴自己拗踢,沒關(guān)系脚牍,等寫完代碼我就把這些東西一并刪了。但是你可能會忘巢墅,以至于在你的最終產(chǎn)品中也會有這樣的代碼诸狭。

用tools吧券膀,別做傻事
以上的情況是可以避免的,我們使用tools命名空間以及其屬性來解決這個問題作谚。

xmlns:tools="http://schemas.android.com/tools"

tools可以告訴Android Studio三娩,哪些屬性在運(yùn)行的時(shí)候是被忽略的庵芭,只在設(shè)計(jì)布局的時(shí)候有效妹懒。比如我們要讓android:text屬性只在布局預(yù)覽中有效可以這樣

<TextView
 android:id="@+id/text_main"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 tools:text="I am a title" />

tools可以覆蓋android的所有標(biāo)準(zhǔn)屬性,將android:換成tools:即可双吆。同時(shí)在運(yùn)行的時(shí)候就連tools:本身都是被忽略的眨唬,不會被帶進(jìn)apk中。

tools屬性的種類
tools屬性可以分為兩種:一種是影響Lint提示的好乐,一種是關(guān)于xml布局設(shè)計(jì)的匾竿。以上介紹的是tools的最基本用法:在UI設(shè)計(jì)的時(shí)候覆蓋標(biāo)準(zhǔn)的android屬性,屬于第二種蔚万。下面介紹Lint相關(guān)的屬性岭妖。

Lint相關(guān)的屬性

tools:ignore
tools:targetApi
tools:locale
tools:ignore

ignore屬性是告訴Lint忽略xml中的某些警告什往。

假設(shè)我們有這樣的一個ImageView

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginStart="@dimen/margin_main"
  android:layout_marginTop="@dimen/margin_main"
  android:scaleType="center"
  android:src="@drawable/divider" />

Lint會提示該ImageView缺少android:contentDescription屬性茶袒。我們可以使用tools:ignore來忽略這個警告:

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginStart="@dimen/margin_main"
  android:layout_marginTop="@dimen/margin_main"
  android:scaleType="center"
  android:src="@drawable/divider"
  tools:ignore="contentDescription" />

tools:targetApi

假設(shè)minSdkLevel 15鳍贾,而你使用了api21中的控件比如RippleDrawable

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
  android:color="@color/accent_color" />

則Lint會提示警告烟逊。

為了不顯示這個警告惜互,可以:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:color="@color/accent_color"
  tools:targetApi="LOLLIPOP" />

tools:locale(本地語言)屬性

默認(rèn)情況下res/values/strings.xml中的字符串會執(zhí)行拼寫檢查茵汰,如果不是英語捐下,會提示拼寫錯誤弧哎,通過以下代碼來告訴studio本地語言不是英語梧田,就不會有提示了淳蔼。

<resources
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  tools:locale="it">
 
  <!-- Your strings go here -->
 
</resources>

這篇文章首先介紹了tools的最基本用法-覆蓋android的屬性,然后介紹了忽略Lint提示的屬性裁眯。下篇文章中鹉梨,我們將繼續(xù)介紹關(guān)于UI預(yù)覽的其他屬性(非android標(biāo)準(zhǔn)屬性)。

ps:關(guān)于忽略Lint的屬性穿稳,如果不想了解的話也沒關(guān)系存皂,因?yàn)椴⒉挥绊懢幾g,一般我都不會管這些警告司草。

第二部分

這部分我們將繼續(xù)介紹關(guān)于UI預(yù)覽的其他屬性(非android標(biāo)準(zhǔn)屬性)艰垂。

tools:context
tools:menu
tools:actionBarNavMode
tools:listitem/listheader/listfooter
tools:showIn
tools:layout

tools:context

context屬性其實(shí)正是的稱呼是activity屬性,有了這個屬性埋虹,ide就知道在預(yù)覽布局的時(shí)候該采用什么樣的主題猜憎。同時(shí)他還可以在android studio的java代碼中幫助找到相關(guān)的文件(Go to Related files)

該屬性的值是activity的完整包名

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.android.example.MainActivity">  <!-- ... -->
</LinearLayout>

tools:menu

Android Studio 使用該屬性。用在布局文件的根元素中搔课,告訴編輯器在 ActionBar 上的菜單內(nèi)容胰柑。取值為定義的 menu 的 id,多個 id 用逗號分隔;還可以使用定義 menu 的 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″ />

其實(shí)預(yù)覽窗口非常智能崩瓤,如果布局和一個activity關(guān)聯(lián)(指上面所講的用tools:context關(guān)聯(lián))它將會自動查詢相關(guān)activity的onCreateOptionsMenu方法中的代碼,以顯示菜單踩官。而menu屬性則可以覆蓋這種默認(rèn)的行為却桶。

你還可以為menu屬性定義多個菜單資源,不同的菜單資源之間用逗號隔開蔗牡。

tools:menu="menu_main,menu_edit"

如果你不希望在預(yù)覽圖中顯示菜單則:

tools:menu=""
最后需要注意颖系,當(dāng)主題為Theme.AppCompat時(shí),這個屬性不起作用辩越。

tools:actionBarNavMode

Android Studio 使用該屬性嘁扼。用在布局文件的根元素中,告訴編輯器在 ActionBar 上的導(dǎo)航模式黔攒。取值為 “standard”趁啸、 “l(fā)ist” 和”tabs” 之一。

<?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:actionBarNavMode=”tabs” />

standard
tabs
list

<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:actionBarNavMode="tabs" />

同樣的督惰,當(dāng)主題是Theme.AppCompat (r21+, at least) 或者Theme.Material,或者使用了布局包含Toolbar的方式不傅。 該屬性也不起作用,只有holo主題才有效姑丑。

listitem, listheader 和listfooter 屬性

顧名思義就是在ListView ExpandableListView等的預(yù)覽效果中添加頭部 尾部 以及子item的預(yù)覽布局蛤签。

<GridView
 android:id="@+id/list"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 tools:listheader="@layout/list_header"
 tools:listitem="@layout/list_item"
 tools:listfooter="@layout/list_footer" />

layout屬性

該屬性由 Android Studio 使用。通常用在 <fragment> 元素中栅哀,用來告訴編輯器該 fragment 使用的是哪個布局文件震肮。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="com.example.fragmenttwopanel.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:layout="@android:layout/list_content" />

tools:showIn
Android Studio 使用該屬性。通常用在被其他布局文件引用的<include> 布局文件中留拾。告訴編輯器該布局文件用在另外一個布局文件中戳晌。例如

<?xml version=”1.0″ encoding=”utf-8″?>
<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” />

除了上面這些屬性以外,標(biāo)準(zhǔn)的 Android 布局屬性都可以當(dāng)做 tools 屬性來使用痴柔。布局編輯器用這些屬性來選擇布局文件沦偎。這些屬性被稱之為 “ **設(shè)計(jì)時(shí)布局屬性 **”,他們只被布局編輯器使用咳蔚,編譯后的代碼中不存在這些內(nèi)容豪嚎。
例如

<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" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"       
    android:paddingBottom="@dimen/activity_vertical_margin"                 
    tools:context=".MainActivity"> 
<TextView    
    android:text="@string/hello_world" 
    tools:text="hello world" 
    android:layout_width="wrap_content" 
    tools:layout_width="400dp" 
    tools:background="@android:color/holo_red_dark" 
    android:layout_height="wrap_content"/>
</RelativeLayout>

上面的布局文件,在布局編輯器中預(yù)覽圖如下:

這樣做的好處是谈火,在編輯布局文件的時(shí)候侈询,不用為了預(yù)覽效果而給真正的屬性設(shè)置一些測試的值,然后當(dāng)程序發(fā)布的時(shí)候糯耍,這些測試的內(nèi)容忘記刪除了扔字。例如為了預(yù)覽 EditText 的顯示效果囊嘉,你把一段文字設(shè)置到 text屬性上:<EditText android:text="John Doe" android:layout_width="wrap_content" android:layout_height="wrap_content" />
當(dāng)你發(fā)布項(xiàng)目的時(shí)候,卻忘記了把該字符串給刪除了革为。 如果這里面包含有比較機(jī)密的內(nèi)容的話扭粱,你的秘密就被泄露出去了。 你可以用

**tools:text="John Doe" **
這樣在發(fā)布的時(shí)候震檩,這些內(nèi)容會自動去掉琢蛤。

設(shè)計(jì)時(shí)布局屬性的限制:

目前只支持標(biāo)準(zhǔn)的屬性,也就是依 android 命名空間下的屬性恳蹲。
目前代碼提示支持的還不是很好虐块,建議先用 android 命名空間來編輯該屬性俩滥,然后把 android 替換為 tools嘉蕾。
設(shè)計(jì)時(shí)布局屬性只能在布局文件中使用,不能在其他資源文件中使用霜旧。
這類還有一些問題需要注意: https://code.google.com/p/android/issues/detail?id=46186

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末错忱,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子挂据,更是在濱河造成了極大的恐慌以清,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件崎逃,死亡現(xiàn)場離奇詭異掷倔,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)个绍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進(jìn)店門勒葱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人巴柿,你說我怎么就攤上這事凛虽。” “怎么了广恢?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵凯旋,是天一觀的道長。 經(jīng)常有香客問我钉迷,道長至非,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任糠聪,我火速辦了婚禮荒椭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘枷颊。我一直安慰自己戳杀,他們只是感情好该面,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著信卡,像睡著了一般隔缀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上傍菇,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天猾瘸,我揣著相機(jī)與錄音,去河邊找鬼丢习。 笑死牵触,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的咐低。 我是一名探鬼主播揽思,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼见擦!你這毒婦竟也來了钉汗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤鲤屡,失蹤者是張志新(化名)和其女友劉穎损痰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體酒来,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卢未,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了堰汉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辽社。...
    茶點(diǎn)故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖衡奥,靈堂內(nèi)的尸體忽然破棺而出爹袁,到底是詐尸還是另有隱情,我是刑警寧澤矮固,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布失息,位于F島的核電站,受9級特大地震影響档址,放射性物質(zhì)發(fā)生泄漏盹兢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一守伸、第九天 我趴在偏房一處隱蔽的房頂上張望绎秒。 院中可真熱鬧,春花似錦尼摹、人聲如沸见芹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽玄呛。三九已至阅懦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間徘铝,已是汗流浹背耳胎。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留惕它,地道東北人怕午。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像淹魄,于是被迫代替她去往敵國和親郁惜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,089評論 25 707
  • 第一部分安卓開發(fā)中揭北,在寫布局代碼的時(shí)候扳炬,ide可以看到布局的預(yù)覽效果。 但是有些效果則必須在運(yùn)行之后才能看見搔体,比如...
    o動感超人o閱讀 828評論 0 50
  • 第一部分安卓開發(fā)中,在寫布局代碼的時(shí)候半醉,ide可以看到布局的預(yù)覽效果疚俱。 一般我們在這樣做的時(shí)候都告訴自己,沒關(guān)系缩多,...
    o動感超人o閱讀 815評論 0 51
  • 我們知道呆奕,在 Android 開發(fā)過程中,我們的數(shù)據(jù)常常來自于服務(wù)端衬吆,只有在運(yùn)行時(shí)才能獲得數(shù)據(jù)展示梁钾,因此在布局 X...
    香辣牛肉面閱讀 8,013評論 3 22
  • 這個國慶節(jié)回家了姆泻,比往常更早一些。待的更久冒嫡,比春節(jié)在家的時(shí)間還長拇勃。 十年前,16歲孝凌,中學(xué)的假期都在田間地頭度過方咆。 ...
    一個景天閱讀 230評論 0 2