Android 开发小工具之:Tools 属性

http://blog.chengyunfeng.com/?p=755#ixzz4apLZhfmi

今天来介绍一些 Android 开发过程中比较有用但是大家又不常用的小工具。这些小工具可以提高 Android 应用开发的效率、还可以提高代码质量。所以还是有必要使用的。

首先介绍布局文件中的 tools 属性。

如果你用 Android Studio 创建一个简单的示例项目,在生成的布局文件中会有这么一行内容:

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

在 tools 命名空间下定义了一些属性就是我们要介绍的 tools 属性。 顾名思义,这些属性都是为编辑工具准备的,具体来说就是 Android Studio 和布局文件编译器(AAPT)。 在开发程序的时候,通过这些属性来告诉编辑器一些额外的信息。

tools:ignore

这个属性是为 Lint 准备的,属性的取值为逗号分隔的 Lint 问题 ID。告诉 Lint 在检测代码的时候,这些问题不用检测。例如:

<string name=”show_all_apps” tools:ignore=”MissingTranslation”>All</string>

tools:targetApi

这个属性也是为 Lint 准备的。和 Java 注解 @TargetApi 一样。取值可以为 Android 版本代号或者对应的整数值。例如:

<GridLayout tools:targetApi=”ICE_CREAM_SANDWICH” >

tools:locale

Lint 和 Android Studio 都会使用该属性。取值为 语言和地区缩写。如果设置该值为非英文类型,则 Studio 不会执行拼写检查。该属性通常出现在资源文件的根元素上。用来告诉 Lint 该文件应该用在那种语言环境中。 例如:values/strings.xml 文件 设置该属性为 es

<resources xmlns:tools=”http://schemas.android.com/tools” tools:locale=”es”>

上面的设置告诉工具,默认的文件夹中的资源为西班牙语而不是英语。

tools:context

Lint 和 Android Studio 都会使用该属性。出现在布局根元素中,用来告诉工具该布局文件和哪个 Activity 关联。这样在设计的时候,布局编辑器会使用相关联的 Activity 的 theme 来渲染该布局文件。取值为 manifests 文件中的 activity name 值一样。例如:

<android.support.v7.widget.GridLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools”tools:context=”.MainActivity” … >

tools:layout

该属性由 Android Studio 使用。通常用在 <fragment> 元素中,用来告诉编辑器该 fragment 使用的是哪个布局文件。

<fragment android:name=”com.example.master.ItemListFragment” tools:layout=”@android:layout/list_content” />

tools:listitem / listheader / listfooter

Android Studio 使用该属性来渲染列表元素。可以用在 AdapterView 的子 View 中。例如 <ListView>、<GridView>、<ExpandableListView> 等。这样在设计的时候,编辑器可以用指定的内容来显示预览内容。

<ListView

android:id=”@android:id/list”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

tools:listitem=”@android:layout/simple_list_item_2″ />

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

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

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

除了上面这些属性以外,标准的 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

Android 开发小工具之:Tools 属性 (转)的更多相关文章

  1. Android 开发—— 小工具,大效率

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:姚志锋 一.Hugo插件 -- 打印方法运行时间 首先申明下,此Hugo非 彼Hugo(Hugo是由Go ...

  2. android 开发小工具收集

    http://blog.csdn.net/tikitoo/article/details/51089422

  3. Android开发常用工具汇总

    Android开发常用工具汇总,本文章不断更新完善 一.数据库小工具Sqlite Developer  SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它的设计目标是嵌入式的, ...

  4. android 桌面小工具(Widget)开发教程

    刚学做了哥Widget,感觉不错哦,先来秀下效果(用朋友手机截的图) 这个Widget会每隔5秒钟自动切换内容和图片,图片最好使用小图,大图会导致你手机桌面(UI)线程卡顿 教程开始: 1.首先创建一 ...

  5. Android 开发常用工具合集

    在 Android 开发中经常使用到的小功能,用于记录开发的那些事^_^ 1. 获取 release 和 debug 版本的 SHA1 public static String getSHA1(Con ...

  6. Android 性能测试小工具 Emmagee

    Emmagee 是一个性能测试小工具 用来监控指定被测应用在使用过程中占用机器的CPU, 内存,流量资源的性能小工具 Emmagee 介绍 Emmagee是网易杭州研究院QA团队开发的一个简单易上手的 ...

  7. [Android Memory] Android性能测试小工具Emmagee

    转载:http://blog.csdn.net/anlegor/article/details/22895993 Emmagee是网易杭州QA团队开发的用于测试指定android应用性能的小工具.该工 ...

  8. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  9. Android开发小问题总结

    Android开发遇到的小问题之小解: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

随机推荐

  1. C++中STL常用容器的区别(转)

    我们常用到的STL容器有vector.list.deque.map.multimap.set和multiset,它们究竟有何区别,各自的优缺点是什么,为了更好的扬长避短,提高程序性能,在使用之前需要我 ...

  2. 关于TP中U方法,在wamp中是绝对路径,在nginx中是相对路径?(坑)

    这个问题已多次遇到,关于tp 框架 使用U 方法跳转, 在Nginx 服务器上可能会遇到路由跳转不过去前面带点(如:./xx) 解决这个问题,可以在tp的入口文件 index.php 里定义个常量 d ...

  3. 正确地使用GIT FORK

    摘自github官方网站,稍后我将抽空翻译. Fork a repo https://help.github.com/articles/fork-a-repo/ Syncing a fork http ...

  4. Light oj 1044 - Palindrome Partitioning(区间dp)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 dp[i][j]表示i到j直接的最小回文区间个数,直接看代码 #include ...

  5. centos7.3 开放端口 防火墙端口

    1. 查看已打开的端口 # netstat -anp 2. 查看想开的端口是否已开 # firewall-cmd --query-port=666/tcp 若此提示 FirewallD is not ...

  6. COFF - 中间文件格式解析

    http://www.cnblogs.com/weikanzong/p/5296739.html

  7. 小菜的系统框架界面设计-XiaoCai.WinformUI代码开源

    我的源码分享 曾经,看到别人漂亮的系统界面,合理的布局,可是却没有提供源码,道理很简单,就是有偿提供,实际上对于有些技巧的东西也并没有多么难,只是不懂原理,感觉到困难罢了. 而对于刚毕业的我,求知欲强 ...

  8. Win7下搭建外网环境的SVN服务器

    最近想跟一帮朋友做点东西,由于几个朋友都身处异地,要想实现版本控制,只能自己搭建一个小的服务器,通过互联网环境来实现版本控制了.本来也在网上找了好多资料,但是总是缺少一些必要的信息,导致最后连接不上服 ...

  9. TOYS-POJ2318

    本题主要是确定给定的点在那块区域.原题给出n条直线,将长方形分为n+1快区域.我们可以对每个给定的点来判断它在那块区域,判段方法可以根据点与直线的位置关系,具体如下,对于点(x0,y0)和直线ax+b ...

  10. python函数式编程学习之map,reduce,filter,sorted

    map(f, list)函数用于将函数f运用到list里的每个元素中 写个例子 def pow(x): return x*x map(pow, [2,3,4]) reduce(f, list)函数用于 ...