您的位置:首页 > 移动开发 > Android开发

android官方技术文档翻译——工具属性

2014-11-26 08:38 423 查看
本文译自androd官方技术文档《Tools Attributes》:http://tools.android.com/tech-docs/tools-attributes

本文地址:http://blog.csdn.net/maosidiaoxian/article/details/41510581。转载请注明出处。翻译如有错讹,敬请指正。


工具属性

Android 有一个专用的XML命名空间,用于使工具可以记录XML文件里的信息,并且在打包程序的进行把信息剥离到不会带来运行时期和下载大小的负面影响的程度。 这个命名空间的 URI 是
http://schemas.android.com/tools
,并且它通常被绑定到
tools:
前缀中:

<FrameLayout 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" >

....


这份文档记录了我们当前使用的工具属性。(注: 这些属性可能会随着时间在以后中改变。)

tools:ignore

此属性可以在任何 XML 元素上设置,它是一个逗号分割的lint 问题ID的列表,表示了应该要在此元素或它的任何子元素上递归忽略的lint问题的ID。

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


使用:
Lint

tools:targetApi

此属性像
Java 类中的 @TargetApi 批注解一样: 它允许您指定一个 API 级别,可以是整数或代码名称,表示此元素需要在此级别之上运行。

<GridLayout
tools:targetApi="ICE_CREAM_SANDWICH" >




使用: Lint

tools:locale

此属性可以设置在资源文件的根元素上,并且应该对应于一种语言或一个地区。这会让工具知道文件的字符串被假定为哪种语言(区域)中的。例如,
values/strings.xml
可以有此根元素:

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


现在我们知道默认值文件夹中的字符串用的语言是西班牙语,而不是英语。

使用:
Lint, Studio (以在非英语资源文件中禁用拼写检查)


tools:context

这个属性通常在一个布局XML文件的根元素中设置,记录了这个布局关联到哪一个activity(因为显然一个布局在设计时可以被多个布局使用)(例如它会用于布局编辑器中以推断默认的主题,由于主题定义在Manifest中,并与activity而不是布局相关联。你可以和在manifests中一样使用点前缀,来指定activity类,而不需要使用完整的程序包名作为前缀。

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


tools:context=".MainActivity"
... >


使用:Studio
和 Eclipse的布局编辑器,Lint

tools:layout

此属性通常设置在一个 <fragment> 标签中,用来记录在设计时你想看到渲染的布局 (在运行时,将由这个标签所列的fragment的类的操作所决定)。

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

使用:
Studio 和 Eclipse的布局编辑器

tools:listitem / listheader / listfooter

这些属性可用于在设计时的 <ListView>(或其他 AdapterView 的子类,比如 <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"
/>


使用:
Studio 和 Eclipse的布局编辑器

tools:showIn

该属性设置于一个被其他布局<include>的布局的根元素上。这让您可以指向包含此布局的其中一个布局,在设计时这个被包含的布局会带着周围的外部布局被渲染。这将允许您“在上下文中”查看和编辑这个布局。需要 Studio 0.5.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"
/>


使用:Studio 的布局编辑器

tools:menu

这个属性在布局的根元素上设置,用于配置在 Action Bar中显示的菜单。Android Studio 通过查看这个布局文件所链接的activity(通过 tools:context)里的onCreateOptionsMenu()方法,尝试找出哪些菜单在 ActionBar 中使用。它允许您重写哪个搜索和显示声明的菜单用于显示。它的值是逗号分隔的
id 列表 (没有 @id/ 或任何这类前缀)。您还可以使用没有.xml 扩展名的菜单xml文件的名称。需要 Studio 0.8.0 或更高版本。


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


使用:Studio 的布局编辑器

tools:actionBarNavMode

这个属性在布局的根元素上设置,用于配置 Action Bar 使用的 导航模式。可能的值包括:“"standard”,“list”和“tabs”。需要
Studio 0.8.0 或更高版本。

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


使用:Studio 的布局编辑器

其他属性: 设计时属性

在布局中,任何其他属性可以是一个内置的
Android 属性别名。例如,这可以让您设置仅设计时的替换文字,用于工具中而不是运行时。详细信息,请参阅设计时布局属性

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: