android中xml tools屬性詳解

第一部分
安卓開(kāi)發(fā)中,在寫(xiě)布局代碼的時(shí)候,ide可以看到布局的預(yù)覽效果。

Paste_Image.png

但是有些效果則必須在運(yùn)行之后才能看見(jiàn),比如這種情況:TextView在xml中沒(méi)有設(shè)置任何字符,而是在activity中設(shè)置了text。因此為了在ide中預(yù)覽效果,你必須在xml中為T(mén)extView控件設(shè)置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" />

一般我們?cè)谶@樣做的時(shí)候都告訴自己,沒(méi)關(guān)系,等寫(xiě)完代碼我就把這些東西一并刪了。但是你可能會(huì)忘,以至于在你的最終產(chǎn)品中也會(huì)有這樣的代碼。

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

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"
  android:textAppearance="@style/TextAppearance.Title"
  android:layout_margin="@dimen/main_margin"
  tools:text="I am a title" />

tools可以覆蓋android的所有標(biāo)準(zhǔn)屬性,將android:換成tools:即可。同時(shí)在運(yùn)行的時(shí)候就連tools:本身都是被忽略的,不會(huì)被帶進(jìn)apk中。
tools屬性的種類(lèi)
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è)我們有這樣的一個(gè)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會(huì)提示該ImageView缺少android:contentDescription屬性。我們可以使用tools:ignore來(lái)忽略這個(gè)警告:

<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會(huì)提示警告。
為了不顯示這個(gè)警告,可以:

<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(本地語(yǔ)言)屬性
默認(rèn)情況下res/values/strings.xml中的字符串會(huì)執(zhí)行拼寫(xiě)檢查,如果不是英語(yǔ),會(huì)提示拼寫(xiě)錯(cuò)誤,通過(guò)以下代碼來(lái)告訴studio本地語(yǔ)言不是英語(yǔ),就不會(huì)有提示了。

<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的屬性,如果不想了解的話也沒(méi)關(guān)系,因?yàn)椴⒉挥绊懢幾g,一般我都不會(huì)管這些警告。

第二部分
這部分我們將繼續(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í)正是的稱(chēng)呼是activity屬性,有了這個(gè)屬性,ide就知道在預(yù)覽布局的時(shí)候該采用什么樣的主題。同時(shí)他還可以在android studio的java代碼中幫助找到相關(guān)的文件(Go to Related files

Paste_Image.png

該屬性的值是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 在預(yù)覽窗口中使用哪個(gè)菜單,這個(gè)菜單將顯示在layout的根節(jié)點(diǎn)上(actionbar的位置)。

Paste_Image.png

其實(shí)預(yù)覽窗口非常智能,如果布局和一個(gè)activity關(guān)聯(lián)(指上面所講的用tools:context關(guān)聯(lián))它將會(huì)自動(dòng)查詢相關(guān)activity的onCreateOptionsMenu方法中的代碼,以顯示菜單。而menu屬性則可以覆蓋這種默認(rèn)的行為。
你還可以為menu屬性定義多個(gè)菜單資源,不同的菜單資源之間用逗號(hào)隔開(kāi)。

tools:menu="menu_main,menu_edit"

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

tools:menu=""

最后需要注意,當(dāng)主題為Theme.AppCompat時(shí),這個(gè)屬性不起作用。

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

  • 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屬性
tools:layout告訴ide,F(xiàn)ragment在程序預(yù)覽的時(shí)候該顯示成什么樣

<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" />
Paste_Image.png

tools:showIn該屬性設(shè)置于一個(gè)被其他布局<include>的布局的根元素上。這讓您可以指向包含此布局的其中一個(gè)布局,在設(shè)計(jì)時(shí)這個(gè)被包含的布局會(huì)帶著周?chē)耐獠坎季直讳秩?。這將允許您“在上下文中”查看和編輯這個(gè)布局。需要 Studio 0.5.8 或更高版本。

關(guān)于tools 就介紹完了。

轉(zhuǎn)載自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0309/2567.html

最后編輯于
?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 第一部分 安卓開(kāi)發(fā)中,在寫(xiě)布局代碼的時(shí)候,AndroidStudio可以看到布局的預(yù)覽效果。 但是有些效果則必須在...
    zivxia閱讀 4,532評(píng)論 1 5
  • 第一部分安卓開(kāi)發(fā)中,在寫(xiě)布局代碼的時(shí)候,ide可以看到布局的預(yù)覽效果。 一般我們?cè)谶@樣做的時(shí)候都告訴自己,沒(méi)關(guān)系,...
    o動(dòng)感超人o閱讀 856評(píng)論 0 51
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,996評(píng)論 25 709
  • 測(cè)試方法: ① 下面有108個(gè)稱(chēng)述. ② 在你認(rèn)為符合你的稱(chēng)述面前做個(gè)記號(hào),注意遮掩每個(gè)稱(chēng)述前面的數(shù)字 ③ 然后把...
    Cherry老師閱讀 781評(píng)論 0 0
  • 我不是最早的妹友,也算不上貼心,聽(tīng)得你們的第一首歌是《你飛到城市另一邊》。那時(shí)候也是刷著微博,就刷到了何老師分享的...
    齊佳楠elina閱讀 369評(píng)論 0 2

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