导读:
Android Studio的布局XML布局文件支持tools命名空间,这个属性主要用于preview预览界面, 构建应用程序时,构建工具将删除这些属性,以便对APK大小或运行时行为没有影响。
这样编写布局就不会像以前那样设置了一些类似text属性.项目完成后,如果没有删除这些数据,用户使用App时就会出现一堆默认的测试数据,提高用户体验
Tools使用:
通常情况下,我们在布局XML文件的根布局,然后根据下面属性介绍对应设置即可,如
<RootTag xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
Tools属性的种类
一、抑制Lint警告信息
- tools:ignore 告诉Lint忽略某些警告(可用于任何元素)
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>
- tools:targetApi (可用于任何元素)
与java中的@TargetApi类似,这里主要用于阻止Lint发出API不匹配警告(实际开发中我们应选择使用兼容版本的组件)
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:targetApi="14" >
- tools:locale (用于resource节点)
本地化属性,这里主要用于告诉元素中的默认语言环境是什么,以避免来自Lint的警告
<resources xmlns:tools="http://schemas.android.com/tools"
tools:locale="es">
二、定义仅在Android Studio 布局预览器中可见的布局特性
tools:text=”123” (用于View节点)
tools:visibility=”gone” 用于View节点
tools:context(用于根布局View)
告诉预览器该采用什么样的主题.(个人感觉没什么用,直接在编辑器工具栏上就能设置预览主题)
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" > //全包名同理
- tools:itemCount=”3” 设置预览器显示的条目数(用于RecycleView)
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="3"/>
- tools:layout 设置预览器预览时Fragment内部绘制的布局(用于Fragmment)
<fragment android:name="com.example.master.ItemListFragment"
tools:layout="@layout/list_content" />
原版
使用了属性
- tools:listitem / tools:listheader / tools:listfooter (用于AdapterView和类似ListView)
设置预览器中显示 列表item,headView和footView
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
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" />
注意: 这些属性在Android Studio 2.2中不适用于ListView,但是在2.3中这个属性是固定的
- tools:showIn (用于被include布局的根布局)
该属性设置于一个被其他布局include的布局的根元素上.我们可以只想任意一个包含此布局的布局
<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" />
这样设置,就可以在TextView中显示这个activity_main的内容
- tools:menu (用于任何View)
此属性指定布局预览器应显示在App Bar中的哪个menu(多个menu用逗号分隔开)
<?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" />
如果不想让当前预览器显示menu,可以用tools:menu=""
主要当主题为Theme.AppCompat时,这个属性不起作用
- tools:minValue / tools:maxValue此属性为NumberPicker View设置最大或最小值(用于)
<NumberPicker xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/numberPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:minValue="0"
tools:maxValue="10" />
- tools:openDrawer (用于DrawerLayout)
此属性可以让我们在布局预览器中打开DrawerLayout,可以通过设置end,left,right,start修改DrawableLayout显示的方式
<android.support.v4.widget.DrawerLayout
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 (用于text文本或Image图像)
该属性可以将占位符数据或图像插入到视图中,解决我们写代码但还没资源时不知道具体UI的尴尬,更多属性查看下表
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="@tools:sample/lorem" />
属性 | 说明 |
---|---|
@tools:sample/full_names | 从@tools:sample / first_names和@tools:sample / last_names的组合中随机生成的全名。 |
@tools:sample/first_names | 设置firstName |
@tools:sample/last_names | 设置lastName |
@tools:sample/cities | 设置世界各地的城市名称 |
@tools:sample/us_zipcodes | 随机生成美国邮政编码 |
@tools:sample/us_phones | 随机生成电话号码,格式如下:(800)555-xxxx |
@tools:sample/lorem | 拉丁语的占位符文本 |
@tools:sample/date/day_of_week | 指定格式的随机日期和时间 |
@tools:sample/date/ddmmyy | 指定格式的随机日期和时间 |
@tools:sample/date/mmddyy | 指定格式的随机日期和时间 |
@tools:sample/date/hhmm | 指定格式的随机日期和时间 |
@tools:sample/date/hhmmss | 指定格式的随机日期和时间 |
@tools:sample/avatars | 设置头像,支持Vector Drawable |
@tools:sample/backgrounds/scenic | 设置Background图片 |
自定义假数据
- 在Android Studio中有这样一个目录 New -> Sample Data Directory
- 这个文件其实就是自定义上面 @tools:sample 的内容
- 这个目录可以存放一些资源文件,如图片资源、json文件、XML文件等等
下面以json文件为例,记录创建完要先运行一下项目
//创建Json文件,加入以下数据
{
"data": [
{
"name": "张三",
"phone": "@tools:sample/us_phones",
"time": "@tools:sample/date/hhmmss",
"avatar": "@tools:sample/avatars"
},
{
"name": "李四",
"phone": "@tools:sample/us_phones",
"time": "@tools:sample/date/hhmmss",
"avatar": "@tools:sample/avatars"
},
{
"name": "赵五",
"phone": "@tools:sample/us_phones",
"time": "@tools:sample/date/hhmmss",
"avatar": "@tools:sample/avatars"
},
{
"name": "王二麻",
"phone": "@tools:sample/us_phones",
"time": "@tools:sample/date/hhmmss",
"avatar": "@tools:sample/avatars"
}
]
}
然后直接在XML中,通过@sample/的方式拿到数据,如 tools:text=”@sample/news.json/data/name” 即可
三、资源压缩
用于启动严格引用检查,并声明在使用资源收缩时是保留还是丢弃某些资源
准备工作
在moudle的build.gradle中设置
android {
...
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
- tools:shrinkMode 严格模式 (用于)
这个属性允许你指定构建工具是否应该使用”安全模式”(保证安全并保留所有明确引用的资源,并且可以通过调用Resources.getIdentifier()) 或 “strict mode” (只保留在代码或其他资源中明确引用的资源)
系统默认使用安全模式(shrinkMode = “safe”),要改为使用严格模式,将shrinkMode =”strict”添加到标记,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:shrinkMode="strict" />
当启用严格模式时,我们可能需要使用tools:keep 保留已删除的资源但实际又需要的资源,并使用tools:discard 以显式删除更多资源.
- tools:keep (用于)
当使用资源收缩来移除未使用的资源时,该属性允许您指定要保留的资源(通常是因为它们在运行时以间接方式引用,例如将动态生成的资源名称传递给Resources.getIdentifier()).
使用时,请使用标记在资源目录(例如:在res/raw/keep.xml中)中创建一个XML文件,并指定每个资源保留在tools:keep属性中作为逗号分隔列表.您可以使用星号作为通配符.如下
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@layout/used_1,@layout/used_2,@layout/*_3" />
- tools:discard (用于resource节点)
当使用资源收缩来去除未使用的资源时,该属性允许您指定要手动放弃的资源(通常是因为资源被引用,但是不会影响您的应用程序,或者由于Gradle插件错误地推断出资源被引用)
使用时,请使用标记在资源目录(例如:在res/raw/keep.xml中)中创建一个XML文件,并指定每个资源保留在tools:discard属性中作为逗号分隔列表.您可以使用星号作为通配符.如下
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:discard="@layout/unused_1" />
补充属性:
用于RecycleView:
- tools:layoutManager=”LinearManager” 布局管理器
- tools:spanCount=”2” 显示列表的行数/列数
- tools:orientation=”horizontal 布局的方向
感觉官方文档有些属性没有标全,建议大家自己多尝试控件里的tools属性
补充的属性都要手动输入完代码才会有显示
总结
本文参考官方文档编写
本篇文章到此结束,欢迎关注,有问题也欢迎评论,共同成长