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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容