javaee论坛

普通会员

225648

帖子

340

回复

354

积分

楼主
发表于 2017-09-08 14:46:44 | 查看: 513 | 回复: 1
开发人员在设计Android Layout布局时,总会伴随着一些乱七八槽的困扰。比如,为了更加逼真的真实数据预览效果,我们在开发时会将TextView的text属性写上一些假数据,而当运行到模拟器或真机上时这些假数据就成了造成体验上甚至测试BUG的脏数据,又需要一一清除。再比如,我们想在XML的预览界面上就看到ListView的Item内容,而不是只有通过编译运行时才能查看。等等,诸如这些存在于开发Layout内容阶段的困扰,都可以通过Tools Attributes得以解决,不妨了解一下。

xmlns 定义


Android提供了一种特殊的tools命名空间,来帮助开发人员在开发Layout布局时使用tools属性解决实时预览等问题。这种属性在应用打包运行时将会被忽略,不产生任何影响。tools命名空间的URI为:http://schemas.android.com/tools,通常可将其定义在Layout的根容器中(在Android Studio中,可利用Live Temples快捷方式,输入toolsNs即可对应提示),如:

<?xml version="1.0" encoding="utf-8"?><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"></RelativeLayout>

这里将Tools Attributes按照功能分为两种类别,一种是去除Lint提示的,一种是展示布局预览的,下面一一介绍相关属性的使用。

Lint 提示


tools:ignore

这个属性用于告诉Android Lint忽视某些xml警告,比如平时使用ImageView的时候都要加上这么一句:android:contentDescription="@null",否则便会出现黄色警告:[Accessibility] Missing contentDescription attribute on image,此时就可以使用tools:ignore属性忽视这个警告:

<ImageView    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:ignore="contentDescription"/>

tools:targetApi

类似Java代码中的@TargetApi注解一样,指定API级别,可以使用数字level,也可以直接用API name,比如elevation属性只能在API 21及更高版本使用,使用tools:targetApi属性可以忽视版本的警告:

<ImageView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    tools:targetApi="LOLLIPOP"/>

tools:locale

指定IDE当前的假定本地语言,可用在资源文件的根元素上,比如用在values/strings.xml上,避免一些拼写语法上的检查:

<resources xmlns:tools="http://schemas.android.com/tools"    tools:local="es">    <!-- string -->    <string name="name">nombre</string></resources>

布局预览


这里也分为两种,一种是替换标准的android命名空间的控件固有属性,举个例子:

<TextView    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    tools:text="Samples"/>

上例中使用tools:text替换了android:text标准属性,在design窗口预览时可以看到TextView的text内容,而运行时则会被忽略。诸如此类,其他android标准属性均可被替换以达到预览效果。另一种就是非android标准属性了,下面一一说明。

tools:context

这个属性用在layout文件的根元素上,指明与当前layout相关联的Activity,从而在预览时使用Activity的主题(theme一般定义在Manifest文件中并且与activities联系在一起,而非layout)。可以使用Activity的全名,也可以利用manifest中定义的包名作为前缀:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context=".MainActivity"    android:layout_width="match_parent"    android:layout_height="match_parent"></RelativeLayout>

tools:layout

这个属性主要用于标签中,指定预览时用的layout布局文件内容:

<fragment    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:layout="@layout/fragment_content"/>

tools:listitem/listheader/listfooter

顾名思义,这三个属性可用于诸如ListView、GridView、ExpandableListView等AdapterView的子类中,实现内容布局的预览。注意:经实践,在Android Studio中已经无法达到这些元素的内容预览效果,但tools:listitem属性可以用在RecyclerView中,比如:

<android.support.v7.widget.RecyclerView    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:listitem="@android:layout/simple_list_item_checked"/>

RecyclerView使用tools:listitem的前后对比预览效果如下:

tools-listitem

tools:showIn

这个属性用在标签所包含的layout的根元素上,指定一个所在的布局文件,这样在被嵌套的layout的design视图中便可以预览到外层的layout内容。比如在activity_main.xml中使用<include>标签嵌套一个include_content.xml文件,那么在include_content.xml文件的根元素中就可以使用tools:showIn属性指定Outer layout,达到在include_content.xml文件中预览activity_main.xml内容的效果:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:showIn="@layout/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"></RelativeLayout>

tools:menu

这个属性用在layout根元素中,指定layout预览时ActionBar展示的menu内容。使用tools:context属性时,ActionBar会自动查找Activity中的onCreateOptionsMenu()方法预览menu,tools:menu属性的设置会覆盖tools:context属性的效果。tools:menu的值只需要使用menu文件的名字即可,多个menu使用逗号隔开,如

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:menu="menu1,menu2"    android:layout_width="match_parent"    android:layout_height="match_parent"></RelativeLayout>

tools:actionBarNavMode

这个属性用在layout根元素中,指定layout预览时ActonBar的导航模式,值有standard、list和tabs三种,如:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:actionBarNavMode="standard"    android:layout_width="match_parent"    android:layout_height="match_parent"></RelativeLayout>

ActionBar不同navigation mode下的预览效果如图所示,关于navigation mode的相关信息可参考Top Level Switching With View Controls

navigation mode

tools:shrinkMode/keep/discard

为了使APK文件尽可能地变小,一般在打包发布时会开启Shrink Code和Shrink Resources的功能,删除项目中无用的代码和资源,使用这三个属性可以指定某些resources的保留与删除,具体信息可以参考官网介绍:Shrink Your Code and Resources

官方参考


以上便是笔者对于Android Tools Attributes的一些理解和实践,英文较好的也可以直接参考官方介绍,地址如下:

http://tools.android.com/tech-docs/tools-attributes

本文由 亦枫 创作并首发于 亦枫的个人博客 ,同步授权微信公众号:技术鸟(NiaoTech)。

欢迎各种形式地交流与转载,注明作者及出处即可。

本文标题为: Android Tools Attributes,让布局设计所见即所得

本文链接为:http://yifeng.studio/2016/10/13/android-tools-attributes/


普通会员

0

帖子

231

回复

233

积分
沙发
发表于 2022-12-21 21:09:05

标记一下

您需要登录后才可以回帖 登录 | 立即注册

触屏版| 电脑版

技术支持 历史网 V2.0 © 2016-2017