android中xml tools屬性詳解

第一部分
安卓開發中,在寫布局代碼的時候,ide可以看到布局的預覽效果。


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

<TextView
  android:id="@+id/text_main"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textAppearance="@style/TextAppearance.Title"
  android:layout_margin="@dimen/main_margin"
  android:text="I am a title" />

一般我們在這樣做的時候都告訴自己,沒關系,等寫完代碼我就把這些東西一并刪了。但是你可能會忘,以至于在你的最終產品中也會有這樣的代碼。

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

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

tools可以告訴Android Studio,哪些屬性在運行的時候是被忽略的,只在設計布局的時候有效。比如我們要讓android:text屬性只在布局預覽中有效可以這樣

<TextView
 android:id="@+id/text_main"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textAppearance="@style/TextAppearance.Title"
 android:layout_margin="@dimen/main_margin"
 tools:text="I am a title" />

tools可以覆蓋android的所有標準屬性,將android:換成tools:即可。同時在運行的時候就連tools:本身都是被忽略的,不會被帶進apk中。
tools屬性的種類
tools屬性可以分為兩種:一種是影響Lint提示的,一種是關于xml布局設計的。以上介紹的是tools的最基本用法:在UI設計的時候覆蓋標準的android屬性,屬于第二種。下面介紹Lint相關的屬性。
Lint相關的屬性

tools:ignore
tools:targetApi
tools:locale

tools:ignore
ignore屬性是告訴Lint忽略xml中的某些警告。
假設我們有這樣的一個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
假設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(本地語言)屬性
默認情況下res/values/strings.xml中的字符串會執行拼寫檢查,如果不是英語,會提示拼寫錯誤,通過以下代碼來告訴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提示的屬性。下篇文章中,我們將繼續介紹關于UI預覽的其他屬性(非android標準屬性)。
ps:關于忽略Lint的屬性,如果不想了解的話也沒關系,因為并不影響編譯,一般我都不會管這些警告。

第二部分
這部分我們將繼續介紹關于UI預覽的其他屬性(非android標準屬性)。

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

tools:context
context屬性其實正是的稱呼是activity屬性,有了這個屬性,ide就知道在預覽布局的時候該采用什么樣的主題。同時他還可以在android studio的java代碼中幫助找到相關的文件(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
告訴IDE 在預覽窗口中使用哪個菜單,這個菜單將顯示在layout的根節點上(actionbar的位置)。


其實預覽窗口非常智能,如果布局和一個activity關聯(指上面所講的用tools:context關聯)它將會自動查詢相關activity的onCreateOptionsMenu方法中的代碼,以顯示菜單。而menu屬性則可以覆蓋這種默認的行為。
你還可以為menu屬性定義多個菜單資源,不同的菜單資源之間用逗號隔開。

tools:menu="menu_main,menu_edit"

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

tools:menu=""

最后需要注意,當主題為Theme.AppCompat時,這個屬性不起作用。

tools:actionBarNavMode
這個屬性告訴ide app bar(Material中對actionbar的稱呼)的顯示模式,其值可以是

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" />

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

listitem, listheader 和listfooter 屬性
顧名思義就是在ListView ExpandableListView等的預覽效果中添加頭部 尾部 以及子item的預覽布局。

<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屬性
tools:layout告訴ide,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該屬性設置于一個被其他布局<include>的布局的根元素上。這讓您可以指向包含此布局的其中一個布局,在設計時這個被包含的布局會帶著周圍的外部布局被渲染。這將允許您“在上下文中”查看和編輯這個布局。需要 Studio 0.5.8 或更高版本。

關于tools 就介紹完了。
注:原文是兩篇文章
Tools of the trade?—?Part 1
Tools of the trade?—?Part 2

覺得完全可以在一篇文章中講完,就翻譯在了一起,原文有很多和內容無關的gif圖,描述也比較啰嗦,都被我去掉了,這篇文章屬于意譯。

轉載:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0309/2567.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,406評論 6 538
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,034評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,413評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,449評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,165評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,559評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,606評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,781評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,327評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,084評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,278評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,849評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,495評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,927評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,172評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,010評論 3 396
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,241評論 2 375

推薦閱讀更多精彩內容

  • 第一部分 安卓開發中,在寫布局代碼的時候,AndroidStudio可以看到布局的預覽效果。 但是有些效果則必須在...
    zivxia閱讀 4,356評論 1 5
  • 第一部分安卓開發中,在寫布局代碼的時候,ide可以看到布局的預覽效果。 但是有些效果則必須在運行之后才能看見,比如...
    o動感超人o閱讀 832評論 0 50
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,692評論 25 708
  • 想到這樣一句話“沒有比較,就沒有傷害”,公平理論也是在對比中得來的。當員工對薪資不滿時,都是比較得來的,與其他品牌...
    王朋彥閱讀 462評論 0 1
  • 出發那天,車站有人在聊大西高鐵的事。 前排一個大姐很是雀躍地向后面——她的丈夫提議"高鐵通車以后我們回家坐高鐵吧,...
    就是王鐸霖閱讀 541評論 0 0