Android 开发小工具之:Tools 属性
http://blog.chengyunfeng.com/?p=755#ixzz4apLZhfmiandroid
今天来介绍一些 Android 开发过程当中比较有用可是你们又不经常使用的小工具。这些小工具能够提升 Android 应用开发的效率、还能够提升代码质量。因此仍是有必要使用的。vim
首先介绍布局文件中的 tools 属性。app
若是你用 Android Studio 建立一个简单的示例项目,在生成的布局文件中会有这么一行内容:编辑器
在 tools 命名空间下定义了一些属性就是咱们要介绍的 tools 属性。 顾名思义,这些属性都是为编辑工具准备的,具体来讲就是 Android Studio 和布局文件编译器(AAPT)。 在开发程序的时候,经过这些属性来告诉编辑器一些额外的信息。工具
这个属性是为 Lint 准备的,属性的取值为逗号分隔的 Lint 问题 ID。告诉 Lint 在检测代码的时候,这些问题不用检测。例如:布局
<string name=”show_all_apps” tools:ignore=”MissingTranslation”>All</string>测试
这个属性也是为 Lint 准备的。和 Java 注解 @TargetApi 同样。取值能够为 Android 版本代号或者对应的整数值。例如:google
<GridLayout tools:targetApi=”ICE_CREAM_SANDWICH” >spa
Lint 和 Android Studio 都会使用该属性。取值为 语言和地区缩写。若是设置该值为非英文类型,则 Studio 不会执行拼写检查。该属性一般出如今资源文件的根元素上。用来告诉 Lint 该文件应该用在那种语言环境中。 例如:values/strings.xml 文件 设置该属性为 es设计
<resources xmlns:tools=”http://schemas.android.com/tools” tools:locale=”es”>
上面的设置告诉工具,默认的文件夹中的资源为西班牙语而不是英语。
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” … >
该属性由 Android Studio 使用。一般用在 <fragment> 元素中,用来告诉编辑器该 fragment 使用的是哪一个布局文件。
<fragment android:name=”com.example.master.ItemListFragment” tools:layout=”@android:layout/list_content” />
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″ />
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 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″ />
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"
这样在发布的时候,这些内容会自动去掉。
设计时布局属性的限制: