利用tools命名空间,android studio支持很多XML属性,当构建app时这些属性会被擦除,对APK的大小和运行时行为没有任何影响。请看官网。

tools属性大致可以分为三类:1,错误处理属性,这样就会影响到Lint的提示;2,资源shrink属性,影响到资源以何种方式shrink;3,布局编辑器设计时View属性。

1 错误处理属性

tools:ignore

任何元素都可以使用这个属性,比如strings.xml里面的string元素,我们可以忽略掉MissingTranslation这个错误属性:

All

再比如对于下面代码中的ImageView元素,如果我们没有添加tools:ignore这个属性,那么Lint会提示该ImageView缺少android:contentDescription属性,tools:ignore="contentDescription"可以忽略这个警告:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/divider"

tools:ignore="contentDescription" />

tools:targetApi

任何元素都可以使用这个属性,它和java代码中的注解@TargetApi是一样的:它指定了当前元素支持API级别,属性值既可以是数字也可以是代码名字。

这样的话,即使指定的minSdkVersion不支持当前元素,那么Lint也不会警告。比如设置的minSdkVersion=12,但是要使用GridLayout的话,就必须加上tools:targetApi="14":

android:layout_height="match_parent"

tools:targetApi="ice_cream_sandwich"/>

tools:locale

只有resources标签才能使用这个属性。resources默认的语言和区域是英语,会进行拼写检查,而这个属性过指定了resources的语言和区域,当然属性值必须是合法的locale qualifier。

比如你可以指定values/strings.xml文件的默认语言是西班牙语而不是英语:

2 资源压缩模式

当使用resource shrinking的时候下面的属性指定资源压缩的一些特性。

为了使用资源压缩,请将build.gradle文件的shrinkResources 属性置为true,(minifyEnabled属性是code shrinking代码压缩):

android {

...

buildTypes {

release {

shrinkResources true

minifyEnabled true

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

tools:shrinkMode

适用对象:。指定构建的时候是使用safe mode还是strict mode。safe mode保留所有直接引用的资源和可能动态使用的资源,比如Resources.getIdentifier()方式调用的资源。strict mode只保留直接引用的资源。

默认的安全模式是shrinkMode="safe"。如果要使用strict mode,可以用下面的代码:

tools:shrinkMode="strict" />

如果启用了strict mode,可以自定义要保留的资源,如果想要保留或舍弃的特定资源,在您的项目中创建一个包含 标记的 XML 文件,并在 tools:keep 属性中指定每个要保留的资源,在tools:discard属性中指定每个要舍弃的资源。这两个属性都接受逗号分隔的资源名称列表。您可以使用星号字符作为通配符。例如:

tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"

tools:discard="@layout/unused2" />

将该文件保存在项目资源中,例如,保存在 res/raw/keep.xml。构建不会将该文件打包到 APK 之中。

更多信息请查看:Shrink your resources。

tools:keep

适用对象:标签。这个属性能够保留特定的资源,比如Resources.getIdentifier())动态使用的资源。用法是可以创建res/raw/keep.xml文件,内容如下:

tools:keep="@layout/used_1,@layout/used_2,@layout/*_3" />

tools:discard

适用对象:标签。有些资源可能被引用但是又对app没有影响,不能直接删除这些资源,那么这个属性可以移除这些资源;或者Gradle插件错误地推断这些资源是被引用的,那么也可以使用这个属性。用法如下:

tools:discard="@layout/unused_1" />

3 布局编辑器设计时View属性

tools:替换android:

tools可以覆盖所有的android属性,相信这个是大家用过的最多的,就不说了。

tools:parentTag

android studio2.2新加了这个属性可以指定merge标签的布局类型,比如:

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

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:parentTag="android.widget.LinearLayout">

tools:context

只有根View才能使用这个属性,它指定了当前布局默认是跟哪个Activity相关联,使得布局获取那个Activity的一些信息比如Activity使用的theme等等,而且当你使用quickfix(mac上的快捷键是alt+enter)给子View添加`onClick事件时,相应的方法代码会插入到这个Activity中。

tools:itemCount

只有RecyclerView才能使用这个属性。我的android studio版本是3.0.1,布局编辑器中RecyclerView显示的默认个数是10个,这个属性可以改变RecyclerView显示的个数。

tools:layout

只有fragment标签能用这个属性,在preview窗口中可以预览这个layout的样式。

tools:listitem / tools:listheader / tools:listfooter

只有AdapterView才能使用这个属性,指定了列表的items、header和footer的布局,不过tools:listitem这个属性RecyclerView也能使用:

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

android:id="@android:id/list"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:listitem="@layout/sample_list_item"

tools:listheader="@layout/sample_list_header"

tools:listfooter="@layout/sample_list_footer" />

tools:showIn

适用对象:被引用的任何根View:

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

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:showIn="@layout/app_bar_main"

tools:itemCount="32">

tools:menu

适用对象:根。告诉IDE 在预览窗口中使用哪个菜单,这个菜单将显示在layout的根节点上(actionbar的位置)。

图片来自http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0309/2567.html

preview窗口很智能,如果布局和一个activity关联(通过tools:context指定)它将会自动查询这个activity的onCreateOptionsMenu方法,以显示菜单,而tools:menu属性则可以覆盖这种默认的行为。

属性的值是menu id,还可以有多个,不同的menu id之间用逗号隔开。

需要注意的是,主题为Theme.AppCompat时,这个属性不起作用。

tools:actionBarNavMode

指定actionbar的显示模式,其值可以是:1. standard、2. tabs、3. list

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主题才有效。参考

tools:minValue / tools:maxValue

适用对象:NumberPicker。预览NumberPicker的最大最小值但是不生效。NumberPicker的maxValue和minValue只能通过代码设置,如果想通过xml设置的话需要另想办法,这里有参考。为什么android studio的xml不支持设置这两个属性呢?我不懂,请大神留言告知。

android:id="@+id/numberPicker"

android:layout_width="match_parent"

android:layout_height="wrap_content"

tools:minValue="0"

tools:maxValue="10" />

tools:openDrawer

适用对象:。让DrawerLayout打开一个方向。

Constant

Value

Description

end

800005

Push object to the end of its container, not changing its size.

left

3

Push object to the left of its container, not changing its size.

right

5

Push object to the right of its container, not changing its size.

start

800003

Push object to the beginning of its container, not changing its size.

xmlns:android="http://schemas.android.com/apk/res/android"

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

android:id="@+id/drawer_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:openDrawer="start" />

"@tools:sample/*" resources

适用对象:所有支持文本和图片的View。这个属性相当于给View加了个placeholder,比如:

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

android:layout_width="wrap_content"

android:layout_height="wrap_content"

tools:text="@tools:sample/lorem" />

下面的表格描述了所有能给View使用的placeholder:

Attribute value

Description of placeholder data

@tools:sample/full_names

Full names that are randomly generated from the combination of @tools:sample/first_names and @tools:sample/last_names.

@tools:sample/first_names

Common first names.

@tools:sample/last_names

Common last names.

@tools:sample/cities

Names of cities from across the world.

@tools:sample/us_zipcodes

Randomly generated US zipcodes.

@tools:sample/us_phones

Randomly generated phone numbers with the following format: (800) 555-xxxx.

@tools:sample/lorem/random

Placeholder text that is derived from Latin.

@tools:sample/date/day_of_week

Randomized dates and times for the specified format.

@tools:sample/date/ddmmyy

同上

@tools:sample/date/mmddyy

同上

@tools:sample/date/hhmm

同上

@tools:sample/date/hhmmss

同上

@tools:sample/avatars

Vector drawables that you can use as profile avatars.

@tools:sample/backgrounds/scenic

Images that you can use as backgrounds.

这些placeholder资源不会打进APK包里面,因此不用担心会增加app体积,只是方便在preview窗口查看预览效果。

下面举两个例子。

比如下面的代码定义了一个RecyclerView,它的tools:listitem="@layout/item_part1":

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

android:id="@+id/recycler_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:listitem="@layout/item_part1" />

再来看下item_part1.xml的代码:

//item_part1.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

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

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/avatar"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_marginEnd="8dp"

android:layout_marginStart="8dp"

android:contentDescription="@null"

app:layout_constraintDimensionRatio="h,1:1"

app:layout_constraintEnd_toStartOf="@id/name"

app:layout_constraintHorizontal_chainStyle="packed"

app:layout_constraintHorizontal_weight="1"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="@id/name"

tools:src="@tools:sample/avatars"/>

android:id="@+id/name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_margin="8dp"

app:layout_constraintBottom_toTopOf="@+id/city"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_weight="5"

app:layout_constraintStart_toEndOf="@id/avatar"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_chainStyle="packed"

tools:text="@tools:sample/full_names"/>

android:id="@+id/city"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="8dp"

app:layout_constraintBottom_toTopOf="@id/description"

app:layout_constraintEnd_toStartOf="@id/date"

app:layout_constraintHorizontal_chainStyle="spread_inside"

app:layout_constraintStart_toEndOf="@id/avatar"

app:layout_constraintTop_toBottomOf="@+id/name"

tools:text="@tools:sample/cities"/>

android:id="@+id/date"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="8dp"

android:gravity="end"

app:layout_constraintBaseline_toBaselineOf="@id/city"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@id/city"

tools:text="@tools:sample/date/ddmmyy"/>

android:id="@+id/description"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_margin="8dp"

android:ellipsize="end"

android:maxLines="3"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@id/avatar"

app:layout_constraintTop_toBottomOf="@+id/city"

tools:text="@tools:sample/lorem/random"/>

从上面的代码可以看到,RecyclerView的item包含5个元素:头像avatar、名字name、城市city、日期date和描述description。头像avatar使用的属性是tools:src="@tools:sample/avatars",名字name使用的属性是tools:text="@tools:sample/full_names",城市city使用的属性是tools:text="@tools:sample/cities",日期date使用的属性是tools:text="@tools:sample/date/ddmmyy",描述description使用的属性是tools:text="@tools:sample/lorem/random",最终的preview窗口如图所示:

例子1的 代码地址

例子1中的5个预览内容都是android studio提供的,能不能自定义呢?答案是可以的。我们首先以名字name为例,我们在Android Studio创建一个sample data directory:

神奇的是android studio自动在当前module目录下生成了sampledata目录而不是我们本以为的res目录下,而且再次右击res目录不能再创建sample data directory,除非把原来的sample data directory删掉才行。

然后我们创建一个“names”文件,每一行加一个名字:

//ampledata/names

Mark Allison

Sir Reginald Fortescue Crumplington-Smythe

Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso

Millicent Marbles

然后我们把原来的placeholder属性tools:text="@tools:sample/full_names"替换一下:

android:id="@+id/name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_margin="8dp"

app:layout_constraintBottom_toTopOf="@+id/city"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_weight="5"

app:layout_constraintStart_toEndOf="@id/avatar"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_chainStyle="packed"

tools:text="@sample/names" />

自定义完名字name,我们再来自定义头像avatar。在sampledata目录下创建avatars目录:

代码使用下面的方式调用:

android:id="@+id/avatar"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_marginStart="8dp"

android:layout_marginEnd="8dp"

android:contentDescription="@null"

app:layout_constraintDimensionRatio="h,1:1"

app:layout_constraintEnd_toStartOf="@id/name"

app:layout_constraintHorizontal_chainStyle="packed"

app:layout_constraintHorizontal_weight="1"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="@id/name"

tools:src="@sample/avatars" />

再来看preview窗口,我们添加了四个头像的VectorDrawable但是preview窗口只显示了2个:

可以看到奇数1和3的头像显示了而偶数2和4并没有,这应该是一个bug,解决办法是1 & 2都用第一个图片,3 & 4都用第二个图片,5 & 6都用第三个图片,7 & 8都用第四个图片:

然后preview窗口就能正确显示我们自定义的头像了:

还有更简单的办法,我们把头像avatar和城市city的内容放到一个json文件中,首先创建一个json文件:

json文件内容如下:

{

"data": [

{

"city": "Hemel Hempstead, Hertfordshire, UK",

"avatar": "@sample/avatars"

},

{

"city": "Brokenwind, Aberdeenshire, UK",

"avatar": "@sample/avatars"

},

{

"city": "Málaga, España",

"avatar": "@sample/avatars"

},

{

"city": "Batchelors Bump, Essex, UK",

"avatar": "@sample/avatars"

}

]

}

然后我们的头像avatar和城市city就能使用这个json文件来引用:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

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

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/avatar"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_marginEnd="8dp"

android:layout_marginStart="8dp"

android:contentDescription="@null"

app:layout_constraintDimensionRatio="h,1:1"

app:layout_constraintEnd_toStartOf="@id/name"

app:layout_constraintHorizontal_chainStyle="packed"

app:layout_constraintHorizontal_weight="1"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="@id/name"

tools:src="@sample/users.json/data/avatar"/>

android:id="@+id/name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_margin="8dp"

app:layout_constraintBottom_toTopOf="@+id/city"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_weight="5"

app:layout_constraintStart_toEndOf="@id/avatar"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_chainStyle="packed"

tools:text="@sample/names"/>

android:id="@+id/city"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="8dp"

app:layout_constraintBottom_toTopOf="@id/description"

app:layout_constraintEnd_toStartOf="@id/date"

app:layout_constraintHorizontal_chainStyle="spread_inside"

app:layout_constraintStart_toEndOf="@id/avatar"

app:layout_constraintTop_toBottomOf="@+id/name"

tools:text="@sample/users.json/data/city"/>

android:id="@+id/date"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="8dp"

android:gravity="end"

app:layout_constraintBaseline_toBaselineOf="@id/city"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@id/city"

tools:text="@tools:sample/date/ddmmyy"/>

android:id="@+id/description"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_margin="8dp"

android:ellipsize="end"

android:maxLines="3"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@id/avatar"

app:layout_constraintTop_toBottomOf="@+id/city"

tools:text="@tools:sample/lorem/random"/>

最终的preview窗口:

例子2的 代码地址

android tools ignore,android tools属性引用相关推荐

  1. android tools ignore,android 中tools:ignore=UselessParent这个属性的含义是什么?

    满意答案 zjpx456 2018.03.05 采纳率:53%    等级:12 已帮助:7021人 这个属性是给lint检查工具看的,这个告诉IDE 以避免显示这样一条消息: "此 Rel ...

  2. 关于tools:ignore=“Suspicious0dp“的问题父类定向为竖向时,别横向等分父类的高

    tools:ignore="Suspicious0dp" 当你查找这个代码的时候,有可能和我犯了一样的错误: 当前LinearLayout的父类布局与设置的子类布局冲突 利用alt ...

  3. Android基础:Android概念

    Android概念 基础概念 第一个Android程序 View 和 ViewGroup Android控件通用属性 基础概念 Android的界面是承载在一个或者多个Activity上. Activ ...

  4. Android - 抑制lint的Android XML的警告:tools:ignore

    抑制lint的Android XML的警告:tools:ignore 本文地址:http://blog.csdn.net/caroline_wendy 本文的合集已经编著成书,高级Android开发强 ...

  5. android xml 里的 app,xml中的android、app、tools你真的了解吗

    在布局文件的编写中,我们经常在跟节点见到这几句代码: 这是怎么样的三兄弟.png 虽然经常见到,但从来没有好好的认识认识,今天我们就来简单的聊一下吧~ 首先我们要明白一点,xmlns是xml name ...

  6. android tools是干什么的,Android 冷兵器 之 tools

    前言 Android开发在所难免的就是UI的预览和调整,一般情况下都是直接run看效果,或者是使用AS的preview预览,但这同样带来个小问题,就是你的测试内容会跟随着代码被打包到apk中,如果没做 ...

  7. Manifest merger failed问题:tools:replace=android:theme和tools:replace=android:icon

    Error:Execution failed for task ':app:processDebugManifest'. Manifest merger failed : Attribute acti ...

  8. Android被忽略的tools

    自动生成的布局xml文件,很多都带有tools字样:但是大部分都被我们删除了: 其实它的作用是让我们这些开发者预览用的,十分的方便: 事例一个TextView: <TextView androi ...

  9. Suggestion: add 'tools:replace=android:value' to meta-data element at AndroidManifest.xml:25:5-2...

    记录下来少走些坑吧 一:不管用 tools:replace="android:icon,android:theme" xmlns:tools="http://schema ...

最新文章

  1. could not find the main class错误
  2. 282021年高考成绩查询方法,往年高考成绩怎么查询 查询成绩的方法
  3. 2021-05-21 深入理解SLAM技术 【4】射影几何--2面中心射影
  4. python安装第三方库win10_在win里anaconda怎么安装第三方的库
  5. 如果去掉数学前后的空格_数学家们是怎么玩趣味拼图游戏的?
  6. Linux 杀死进程方法大全(kill,killall)
  7. 阿里巴巴架构师,讲透2亿用户的钉钉系统架构实践
  8. 图画日记怎么画_画画日记(通用10篇)
  9. AMOS从零到基本操作入门(转载)
  10. MaxScale 2.5
  11. 教育行业运维审计用什么堡垒机好?有什么作用?
  12. 实战例子:Solidity代码小失误导致池子里60万U被盗空
  13. 十二个一的宠物倾向分析
  14. 饿了么商家开放平台踩坑记录1,php更新商品信息提示业务异常BUSINESS_ERROR by勤勤学长 318692996
  15. 第一个将Palette Mode引入VVC,阿里云在JVET会议上引起关注
  16. VMware教程:设置 CentOS 7 共享文件夹
  17. MFC ---- CString
  18. Accept和Content-type的意思
  19. 软件项目开发基本流程详解
  20. 人脸识别用哪种python库_GitHub - Veechiry/face_collection: python的face_recognition人脸识别库的使用...

热门文章

  1. shell脚本模拟键盘输入完成程序交互(定时重启docker部署的项目样例)
  2. 快来试试Python写的游戏《我的世界》
  3. 新手如何做游戏代理赚钱?
  4. 山水印|竹林野茶:在这个临近八月中秋的九月,再喝桂花香茶
  5. 【浏览器】浏览器缓存
  6. 集合类超级无敌史无前例的超详细总结
  7. 为什么无线AP标称的速率和实际速率不一致?(空口速率)
  8. ICV:超声波雷达迎来数字化变革,2026年全球市场规模将达145亿美元
  9. 【极客时间-SQL必知必会】学习笔记1-了解SQL
  10. java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字