Tools属性Tools Attributes

来源:互联网 发布:Linux cat退出 编辑:程序博客网 时间:2024/05/29 15:16

Android有一个专门的工具类XML命名空间,用于在XML文件中记录信息,而且这些信息会在程序打包时被去掉,所以不会造成运行时和下载的负担。这个命名空间的URI是http://schemas.android.com/tools,常用前缀是tools:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"<strong>xmlns:tools="http://schemas.android.com/tools"</strong>android:layout_width="match_parent"android:layout_height="match_parent" >....
这篇文档记录了我们常用的工具类属性(这可能会经常变

化)。

tools:ignore

这个属性可以应用在任何XML元素上,它的值是用逗号分隔的Lint问题ID,表示该元素及其子元素都忽略这些问题。

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

用于:Lint.

tools:targetApi

这个属性就像Java中的@TargetApi注解。它让你指定该元素运行时的API级别,可以是API的数值,也可以示API的名字。

<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >

用于:Lint.

tools:locale

该属性可以用在资源Value文件的跟元素上,它对应一种语言(可以加上一个区域)。这样工具类就知道文件中的字符串究竟是哪个语言(或者区域的)。举个例子,values/strings.xml 可以这样指定跟元素

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

这样我们就知道缺省Values文件夹中的使用的语音是西班牙语而不是英语。

用于:Lint,Studio(关闭资源文件中的拼写检查).

其他的就没翻译了:

tools:context

用于layout猜测布局预览时使用的主题。因为主题一般在Manifest文件中指定,而且主题之和Activity关联,而不是layout。

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

tools:layout

用在<fragment>标签上,用于指定设计时设计器渲染的layout。

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

其他的,就不多说了:

tools:listitem/listheader/listfooter

tools:showIn

tools:menu

tools:actionBarNavMode

Designtime Layout Attributes

可以用tools:xxxxx属性指定设计时的android:xxxxx属性。

首先得在布局文件中加上命名空间:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" <strong> xmlns:tools="http://schemas.android.com/tools"</strong>android:layout_width="match_parent"...

举个例子:想在设计时,让TextView显示“aaa”,而在正式运行时不显示该字符串。只要这样:

<TextView<strong> tools:text="aaa"</strong>android:layout_width="wrap_content"android:layout_height="wrap_content" />
0 0
原创粉丝点击