Android 中xml tools屬性詳解

第一部分

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

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

<TextView
  android:id="@+id/text_main"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  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"
 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

Android Studio 使用該屬性。用在布局文件的根元素中,告訴編輯器在 ActionBar 上的菜單內容。取值為定義的 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″ />

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

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

tools:menu="menu_main,menu_edit"

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

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

tools:actionBarNavMode

Android Studio 使用該屬性。用在布局文件的根元素中,告訴編輯器在 ActionBar 上的導航模式。取值為 “standard”、 “list” 和”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" />

同樣的,當主題是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屬性

該屬性由 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” />

除了上面這些屬性以外,標準的 Android 布局屬性都可以當做 tools 屬性來使用。布局編輯器用這些屬性來選擇布局文件。這些屬性被稱之為 “ **設計時布局屬性 **”,他們只被布局編輯器使用,編譯后的代碼中不存在這些內容。
例如

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

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

這樣做的好處是,在編輯布局文件的時候,不用為了預覽效果而給真正的屬性設置一些測試的值,然后當程序發布的時候,這些測試的內容忘記刪除了。例如為了預覽 EditText 的顯示效果,你把一段文字設置到 text屬性上:<EditText android:text="John Doe" android:layout_width="wrap_content" android:layout_height="wrap_content" />
當你發布項目的時候,卻忘記了把該字符串給刪除了。 如果這里面包含有比較機密的內容的話,你的秘密就被泄露出去了。 你可以用

**tools:text="John Doe" **
這樣在發布的時候,這些內容會自動去掉。

設計時布局屬性的限制:

目前只支持標準的屬性,也就是依 android 命名空間下的屬性。
目前代碼提示支持的還不是很好,建議先用 android 命名空間來編輯該屬性,然后把 android 替換為 tools。
設計時布局屬性只能在布局文件中使用,不能在其他資源文件中使用。
這類還有一些問題需要注意: https://code.google.com/p/android/issues/detail?id=46186

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容