Android 布局文件中的tools属性

综述

开发了这么久android,现在才开始总结tools属性,我感觉到羞愧。
tools属性官方文档的地址

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

tools属性在使用的时候需要声明namespace的Uri如下:

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

tools属性列表如下

tools:ignore
tools:targetApi
tools:locale
tools:context
tools:layout
tools:listitem / listheader / listfooter
tools:showIn
tools:menu
tools:actionBarNavMode
tools:shrinkMode
tools:keep
tools:discard

除此之外还有Designtime Layout Attributes专门用于设计状态的属性。这一部分文档在这里

http://tools.android.com/tips/layout-designtime-attributes

各个属性的意义

tools:ignore

This attribute can be set on any XML element, and is a comma separated list of lint issue ID’s that should be ignored on this element or any of its children, recursively.
这个属性可以再xml元素中设置,是一个用逗号隔开的事件ID的列表,这些事件ID会在元素以及他的子元素中被忽略。也就是说设置了相关的事件ID之后,这些事件ID就不会再lint中报错。例如

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

tools:targetApi

This attribute is like the @TargetApi annotation in Java classes: it lets you specify an API level, either as an integer or as code name, that this element is known to be running on.
这个属性类似于在Java类中的@TargetApi声明。这个属性可以让你实例化这个元素想运行的API等级,不论是用名字还是用数字。

<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >

tools:locale

This attribute can be set on the root element in a resource value file and should correspond to a language and optionally a region. This will let tools know what language (locale) the strings in the file are assumed to be. For example, values/strings.xml can have this root element:
这个属性可以在资源文件中的根元素中设置并且应该对应一个语言或者地区。这个会让工具了解你文件中的strings是那种语言。例如,在values/strings.xml可以有如下的根元素

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

tools:context

This attribute is typically set on the root element in a layout XML file, and records which activity the layout is associated with (at designtime, since obviously a layout can be used by more than one layout). This will for example be used by the layout editor to guess a default theme, since themes are defined in the Manifest and are associated with activities, not layouts. You can use the same dot prefix as in manifests to just specify the activity class without the full application package name as a prefix。
这个属性一般设置在布局文件的根元素中,记录这个布局是和哪个Activity联系的(在设计的时候,因为一个布局明显可以被不止一个布局使用)。这个可以在layout editors中使用去猜测默认的主题,因为主题定义在Manifest文件中,并且和activity而不是和布局联系在一起。你可以使用在manifest中小点后面的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" ... >

tools:layout

This attribute is typically set in a tag and is used to record which layout you want to see rendered at designtime (at runtime, this will be determined by the actions of the fragment class listed by the tag).
这个属性一般在fragment标签中设置,用于记录你在设计状态想渲染哪一个布局。(在运行时,渲染由tag排列的fragment类的actions决定)也就是说仅仅是设计状态有效,在运行状态由其他方式决定。

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

tools:listitem / listheader / listfooter

These attributes can be used on a (or other AdapterView children like , etc) to specify layouts to use for the list items, as well as list headers and list footers, at designtime. The tool will fill in dummy data to show a list with some representative contents.
这些属性可以用在ListView标签下,或者其他AdapterView的子类例如GridVIew,ExpandablelistView等,用于在设计的时候设置布局的item,list headers和list footers。这个工具会填充虚假数据和有代表性的内容去展示一个list。

 <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

Attribute set on the root element of a layout that ’ed by another layout. This allows you to point to one of the layouts which includes this layout, and at designtime this included layout will be rendered with the outer layout surrounding it. That allows you to view and edit the layout “in context”. Requires Studio 0.5.8 or later. More information in the release announcement.
这个属性在布局的根标签下,这个布局必须被其他布局用include包含。这个允许你在布局中指出布局中某一个布局被其他布局包含,并且在设计时,被包含的布局可以被外面包含的布局渲染。这可以 让你直接在context里面进行布局。需要studio 0.5.8以上的版本。

<?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

Attribute set on the root element of a layout to configure the menus to be shown in the Action Bar. Android Studio tries to figure out which menus to use in the ActionBar by looking at the onCreateOptionsMenu() method in the activity linked to the layout (by tools:context). This allows you to override that search and explicitly state which menus to show. The value is a comma separated list of ids (without @id/ or any such prefix). You can also use the file names of the menu xml without the .xml extension. Requires Studio 0.8.0 or later.
在布局的根元素下配置用于配置应该在ActionBar的菜单。Android studio通过和布局文件联系起来的Activity中onCreateOptionsMenu()函数弄清楚在ActionBar中选择哪一个Menu。这允许你重写菜单需要显示的搜索和明确状态。这个值是用逗号分割的id列表(不用@id或者类似的前缀)。你也可以使用menu布局文件的名字,不用带xml扩展名。需要Android studio 0.8.0或这以上版本。

<?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

Attribute set on the root element of a layout to configure the navigation mode used by the Action Bar. Possible values include: “standard”, “list” and “tabs” Requires Studio 0.8.0 or later.
在布局的根元素中设置用于ActIonBar配置导航栏模式。可能的值包含 “standard” “list” “tabs” 要求AndroidStudio版本0.8.0或这往上。

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

tools:shrinkMode

When using resource shrinking to automatically strip out unused resources, you can specify whether the Gradle plugin should play it safe and whitelist all resources that might be referenced via a getIdentifier lookup from string resources, or whether it should only consider resources explicitly listed in code and in other resources. The default is to play it safe; this is shrinkMode=”safe”. To instead have the Gradle plugin only consider explicitly referenced resources, use shrinkMode=”strict”. In that case, you can use the tools:keep and tools:discard attributes to explicitly list resources to keep or remove.
当使用资源压缩自动剔除无用的资源。你可以决定gradle插件应该默认为safe并且把所有在string资源中通过getIdentifier函数引用寻找到的资源加入白名单,或者只考虑在代码或者其他资源中列出的资源。默认为safe。如果想更改gradle plugin为明确引用模式,只要吧shrinkMode设置为”strict”。在那种情况下,不可以使用tools:keep和tools:discard属性来明确列出想要保留和移除的资源。

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:shrinkMode="safe"
    tools:discard="@layout/unused2" />

tools:keep

When using resource shrinking to automatically strip out unused resources, and when using shrinkMode=”strict”, you can specify specific resources to keep (typically because they are referenced in an indirect way via runtime code, such as Resources#getIdentifier(some dynamically computed resource name).

The value is a comma separated list of resource references (and a globbing pattern is allowed.)
使用资源压缩来自动剔除无用的资源的时候,并且shrinkMode设置为strict,你可以设置资源为keep(一般这些资源没有直接被正在运行的代码引用,例如动态计算资源的名称)。
这个值是用逗号分隔的资源引用列表,(可以使用通配符)

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*" />

tools:discard

When using resource shrinking to automatically strip out unused resources, you can specify specific resources to discard (typically because the resource is referenced in some way that you know does not affect your app, or the Gradle plugin has incorrectly deduced that the resource is referenced (which can happen because resource identifiers are inlined by the compiler, so the resource analyzer can’t tell the difference between a genuinely referenced resource and an integer value in the program that happens to have the same value.)

The value is a comma separated list of resource references (and a globbing pattern is allowed.)
当使用资源压缩自动剔除无用的资源的时候,你可以指定一些资源为discard(一般是因为资源以某种你知道不会影响你的应用的方式引用或者Gradle插件错误推倒出这个资源被引用了(因为资源标识符被编译期内联,所以资源分析器不知道真正的引用资源和程序中刚好有值相等的整型值之间的区别))
这个值是用逗号隔开的资源引用列表(可以使用通配符)。

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:discard="@layout/unused2" />

Designtime Layout Attributes

Designtime layout attributes的意思就是在设计状态时使用的工具。
在设计界面的时候,为了直观看设计效果,可以先用tools来替代android用来查看效果。并且这些属性在运行的时候会自动忽略。这个功能要求Android studio的版本要在0.2.11或者以上。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值