Android Studio Tools Attributes Reference

来源:互联网 发布:程序员实用算法书评 编辑:程序博客网 时间:2024/06/08 03:52

Tools Attributes Reference

Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
Android Studio提供了一系列的xml属性关于tools命名空间的,可以在设计时(例如指定哪一个layout布局来展示在一个fragment中)或者是编译时期行为(例如xml资源使用哪一种压缩模式)当你build你的app 时,build tools会移除这些属性,这样就不会影响apk的大小或者说运行时行为。
To use these attributes, add the tools namespace to the root element of each XML file where you’d like to use them, as shown here:

<RootTag xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" >

为了使用这些属性,把你想使用的属性添加在xml文件的父布局中。

Error handling attributes 处理错误的属性

The following attributes help suppress lint warning messages.
tools:ignore
Intended for: Any element 用于任何元素
Used by: Lint 被Lint工具调用
This attribute accepts a comma-separated list of lint issue ID’s that you’d like the tools to ignore on this element or any of its decendents.
这个属性接受一个lint id的列表,包含你想忽略的元素或者是他说支持的所有元素,就是说你在xml文件中声明出来的id,lint 工具就不会再检测改问题。
For example, you can tell the tools to ignore the MissingTranslation error:

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

tools:targetApi
Intended for: Any element
Used by: Lint
This attribute works the same as the @TargetApi annotation in Java code: it lets you specify the API level (either as an integer or as a code name) that supports this element.
This tells the tools that you believe this element (and any children) will be used only on the specified API level or higher. This stops lint from warning you if that element or its attributes are not available on the API level you specify as your minSdkVersion.
这个属性和在代码中@TargetApi注解 是一样的,你可以指定能够支持这个元素属性的API级别(一个整数或者是一个名称)
For example, you might use this because GridLayout is available only on API level 14 and higher, but you know this layout is not used for any lower versions:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:targetApi="14" >

However, you should instead use GridLayout from the support library.
tools:locale
Intended for:
Used by: Lint, Android Studio editor
This tells the tools what the default language/locale is for the resources in the given element (because the tools otherwise assume English) in order to avoid warnings from the spell checker. The value must be a valid locale qualifier.
在标签里面,指定资源的默认语言(因为默认语言认定为时英语)来避免拼写检查,这个值必须是一个合法的名称,
For example, you can add this to your values/strings.xml file (the default string values) to indicate that the language used for the default strings is Spanish rather than English:

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

Design-time view attributes 设计时期 view属性

The following attributes define layout characteristics that are visible only in the Android Studio layout preview.
tools: instead of android:
Intended for:
Used by: Android Studio layout editor
You can insert sample data in your layout preview by using the tools: prefix instead of android: with any attribute from the Android framework. This is useful when the attribute’s value isn’t populated until runtime but you want to see the effect beforehand, in the layout preview.
如果你想提前看效果,你可以用tools 代替android命名空间 插入一个样本数据在预览版的layout,这是一个很有用的属性但是不会再运行时出现。
For example, if the android:text attribute value is set at runtime or you want to see the layout with a value different than the default, you can add tools:text to specify some text for the layout preview only.
比如说 用 tools:text=””代替 android:text=””;

Figure 1. The tools:text attribute sets “Google Voice” as the value for the layout preview
You can add both the android: namespace attribute (which is used at runtime) and the matching tools: attribute (which overrides the runtime attribute in the layout preview only).
You can also use a tools: attribute to unset an attribute only for the layout preview. For example, if you have a FrameLayout with multiple children but you want to see only one child in the layout preview, you can set one of them to be invisible in the layout preview, as shown here:

<Button    android:id="@+id/button"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="First" /><Button    android:id="@+id/button2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Second"    tools:visibility="invisible"  />

When using the Layout Editor in design view, the Properties window also allows you to edit some design-time view attributes. Each design-time attribute is indicated with a wrench icon next to the attribute name to distinguish it from the real attribute of the same name.
和 上个Text 值是一个道理。
tools:context
Intended for: Any root
Used by: Lint, Android Studio layout editor
This attribute declares which activity this layout is associated with by default. This enables features in the editor or layout preview that require knowledge of the activity, such as what the layout theme should be in the preview and where to insert onClick handlers when you make those from a quickfix (figure 2)
Tools:context 指明某个Activity 默认关联使用的布局,在布局编辑器或者是预览布局,这个特性可以让指定的Activity具备一定的能力,比如Activity的主题在预览布局是什么样,知道在哪个Activity插入onClick处理事件。

Figure 2. Quickfix for the onClick attribute works only if you’ve set tools:context
You can specify the activity class name using the same dot prefix as in the manifest file (excluding the full package name). For example:

<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context=".MainActivity" >

Tip: You can also select the theme for the layout preview from the Layout Editor toolbar.
tools:layout
Intended for:
Used by: Android Studio layout editor
This attribute declares which layout you want the layout preview to draw inside the fragment (because the layout preview cannot execute the activity code that normally applies the layout).
For example:

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

在Fragment中预览指定的布局
tools:listitem / tools:listheader / tools:listfooter
Intended for: (and subclasses like )
Used by: Android Studio layout editor
These attributes specify which layout to show in the layout preview for a list’s items, header, and footer. Any data fields in the layout are filled with numeric contents such as “Item 1” so that the list items are not repetitive.
For example:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@android:id/list"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:listitem="@layout/sample_list_item"    tools:listheader="@layout/sample_list_header"    tools:listitem="@layout/sample_list_footer" />

Note:
These attributes don’t work for ListView in Android Studio 2.2, but this is fixed in 2.3 (issue 215172).
在ListView中预览 头信息 item等
tools:showIn
Intended for: Any root in a layout that’s referred to by an
Used by: Android Studio layout editor
This attribute allows you to point to a layout that uses this layout as an include, so you can preview (and edit) this file as it appears while embedded in its parent layout.
For example:

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

Now the layout preview shows this TextView layout as it appears inside the activity_main layout.
tools:menu
Intended for: Any root
Used by: Android Studio layout editor
This attribute specifies which menu the layout preview should show in the app bar. The value can be one or more menu IDs, separated by commas (without @menu/ or any such ID prefix and without the .xml extension). For example:

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

Resource shrinking attributes压缩资源属性

The following attributes allow you to enable strict reference checks and declare whether to keep or discard certain resources when using resource shrinking.
To enable resource shrinking, set the shrinkResources property to true in your build.gradle file (alongside minifyEnabled for code shrinking). For example:

android {    ...    buildTypes {        release {            shrinkResources true            minifyEnabled true            proguardFiles getDefaultProguardFile('proguard-android.txt'),                    'proguard-rules.pro'        }    }}

tools:shrinkMode
Intended for: 在 标签中使用
Used by: Build tools with resource shrinking
This attribute allows you to specify whether the build tools should use “safe mode” (play it safe and keep all resources that are explicitly cited and that might be referenced dynamically with a call to Resources.getIdentifier()) or “strict mode” (keep only the resources that are explicitly cited in code or in other resources).
这个属性允许你指定是否是用安全模式(运行安全并且保持所有的明确引用的资源和可能动态调用Resources.getIdentifier() 的资源)或者是严格模式(只保持在代码或者其他地方明确引用的资源)
The default is to use safe mode (shrinkMode=”safe”). To instead use strict mode, add shrinkMode=”strict” to the tag as shown here:
默认情况是使用safe模式

<?xml version="1.0" encoding="utf-8"?><resources xmlns:tools="http://schemas.android.com/tools"    tools:shrinkMode="strict" />

When you enable strict mode, you may need to use tools:keep to keep resources that were removed but that you actually want, and use tools:discard to explicitly remove even more resources.
For more information, see Shrink your resources.
tools:keep
Intended for:
Used by: Build tools with resource shrinking
When using resource shrinking to remove unused resources, this attribute allows you to specify resources to keep (typically because they are referenced in an indirect way at runtime, such as by passing a dynamically generated resource name to Resources.getIdentifier()).
当你使用压缩工具来移除没有使用的资源。这个属性允许你指定你想保持的资源(比较典型的原因 他们在运行期间被引用或者被别的方法引用)
To use, create an XML file in your resources directory (for example, at res/raw/keep.xml) with a tag and specify each resource to keep in the tools:keep attribute as a comma-separated list. You can use the asterisk character as a wild card. For example:

<?xml version="1.0" encoding="utf-8"?><resources xmlns:tools="http://schemas.android.com/tools"    tools:keep="@layout/used_1,@layout/used_2,@layout/*_3" />

For more information, see Shrink your resources.
tools:discard
Intended for:
Used by: Build tools with resource shrinking
When using resource shrinking to strip out unused resources, this attribute allows you to specify resources you want to manually discard (typically because the resource is referenced but in a way that does not affect your app, or because the Gradle plugin has incorrectly deduced that the resource is referenced).

允许你使用 discard 手动的移除一些没有使用的资源
To use, create an XML file in your resources directory (for example, at res/raw/keep.xml) with a tag and specify each resource to keep in the tools:discard attribute as a comma-separated list. You can use the asterisk character as a wild card. For example:

<?xml version="1.0" encoding="utf-8"?><resources xmlns:tools="http://schemas.android.com/tools"    tools:discard="@layout/unused_1" />
0 0
原创粉丝点击