【翻译】(28)提供资源

【翻译】(28)提供资源

 

see

http://developer.android.com/guide/topics/resources/providing-resources.html

 

原文见

http://developer.android.com/guide/topics/resources/providing-resources.html

 

-------------------------------

 

Providing Resources

 

提供资源

 

Quickview

 

快速概览

 

* Different types of resources belong in different subdirectories of res/

 

* 不同类型资源属于res/的不同子目录

 

* Alternative resources provide configuration-specific resource files

 

* 可选资源提供配置特定的资源文件

 

* Always include default resources so your app does not depend on specific device configurations

 

* 总是包含默认资源,所以你的应用不依赖于特定设备配置

 

In this document

 

本文目录

 

* Grouping Resource Types 分组资源类型

* Providing Alternative Resources 提供可选资源

* Qualifier name rules 修饰符命名规则

* Creating alias resources 创建别名资源

* Providing the Best Device Compatibility with Resources 提供资源的最佳设备兼容性

* Providing screen resource compatibility for Android 1.5 提供Android 1.5的屏幕资源兼容性

* How Android Finds the Best-matching Resource Android如何找到最佳匹配的资源

* Known Issues 已知问题

 

See also

 

另见

 

Accessing Resources 访问资源

Resource Types 资源类型

Supporting Multiple Screens 支持多屏

 

-------------------------------

 

You should always externalize application resources such as images and strings from your code, so that you can maintain them independently. You should also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. At runtime, Android uses uses the appropriate resource based on the current configuration. For example, you might want to provide a different UI layout depending on the screen size or different strings depending on the language setting.

 

你应该总是从你的代码中外部化应用程序资源诸如图片和字符串,使你可以独立地维护它们。你还应该为特定设备配置提供可选资源,通过在特别命名的资源目录中分组它们来做到。在运行期,Android基于当前配置使用(注:疑有误,多了一个uses)合适的资源。例如,你可能希望提供不同用户界面布局,依赖于屏幕大小,或者不同的字符串,依赖于语言设置。

 

Once you externalize your application resources, you can access them using resource IDs that are generated in your project's R class. How to use resources in your application is discussed in Accessing Resources. This document shows you how to group your resources in your Android project and provide alternative resources for specific device configurations.

 

一旦你外部化你的应用程序资源,你可以使用生成在你的工程的R类中的资源ID访问它们。如何使用你的应用程序中的资源在访问资源中讨论。本文向你展示如何在你的Android工程中分组你的资源以及为特定设备配置提供可选资源。

 

-------------------------------

 

Grouping Resource Types

 

分组资源类型

 

You should place each type of resource in a specific subdirectory of your project's res/ directory. For example, here's the file hierarchy for a simple project:

 

你应该把每类资源放在你的工程的res/目录的一个特定子目录下。例如,这里是一个简单工程的文件层级:

 

-------------------------------

 

MyProject/

    src/  

        MyActivity.java  

    res/

        drawable/  

            icon.png  

        layout/  

            main.xml

            info.xml

        values/  

            strings.xml 

 

-------------------------------

 

As you can see in this example, the res/ directory contains all the resources (in subdirectories): an image resource, two layout resources, and a string resource file. The resource directory names are important and are described in table 1.

 

正如你可以在这个示例中看到的那样,res/目录包含所有资源(在子目录下):一个图片资源,两个布局资源,以及一个字符串资源文件。资源目录的名称非常重要,在表1中描述。

 

Table 1. Resource directories supported inside project res/ directory.

 

表1. 支持在工程res/目录中的资源目录。

 

-------------------------------

 

Directory Resource Type

 

目录 资源类型

 

* animator/ XML files that define property animations.

 

* animator/ XML文件,定义属性动画(注:可能是指那些可以用各种属性值控制的动画,区别于帧动画?)。

 

* anim/ XML files that define tween animations. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.)

 

* anim/ XML文件,定义补间动画。(属性动画也可以保存在此目录中,但animator/倾向于属性动画以区分两种类型。)

 

* color/ XML files that define a state list of colors. See Color State List Resource

 

* color/ XML文件,定义一个颜色的状态列表。参见颜色状态列表资源

 

* drawable/

 

* drawable/

 

Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes:

 

位图文件(.png,.9.png,.jpg,.gif)或XML文件,被编译成以下可绘画资源子类型:

 

* Bitmap files 位图文件

* Nine-Patches (re-sizable bitmaps) 九宫(可重新改变大小的位图)

* State lists 状态列表

* Shapes 图形

* Animation drawables 动画可绘画对象

* Other drawables 其它可绘画对象

 

See Drawable Resources.

 

参见可绘画资源。

 

* layout/ XML files that define a user interface layout. See Layout Resource.

 

* layout/ XML文件,定义一个用户界面布局,参见布局资源。

 

* menu/ XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource.

 

* menu/ XML文件,定义应用程序菜单,诸如一个选项菜单,上下文菜单,或子菜单。参见菜单资源。

 

* raw/

 

* raw/

 

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

 

任意以它们原始形式保存的文件。为了用一个原始的InputStream打开这些资源,用R.raw.filename里的资源ID调用Resources.openRawResource()。

 

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

 

然而,如果你需要访问原来的文件名和文件层级,你可以考虑保存一些资源在assets/目录中(而非res/raw/)。在assets/中的文件不赋予一个资源ID,所以你可以只使用AssetManager读取它们。

 

* values/

 

* values/

 

XML files that contain simple values, such as strings, integers, and colors.

 

XML文件,包含简单值,诸如字符串、整型,以及颜色。

 

Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename, files in the values/ directory describe multiple resources. For a file in this directory, each child of the <resources> element defines a single resource. For example, a <string> element creates an R.string resource and a <color> element creates an R.color resource.

 

尽管其它res/子目录中的XML资源文件定义一个基于XML文件名的单一资源,但是values/目录中的文件描述多个资源。对于此目录中的一个文件,每个<resources>元素的子元素定义一个单一资源。例如,一个<string>元素创建一个R.string资源,而一个<color>元素创建一个R.color资源。

 

Because each resource is defined with its own XML element, you can name the file whatever you want and place different resource types in one file. However, for clarity, you might want to place unique resource types in different files. For example, here are some filename conventions for resources you can create in this directory:

 

因为每个资源用它自己的XML元素来定义,所以你可以把文件命名为你希望的任何名称,并且放置不同的资源类型在一个文件。然而,为清晰起见,你可能希望放置唯一资源类型在不同的文件中。例如,这里有你可以在这个目录中创建的一些资源的文件名约定:

 

* arrays.xml for resource arrays (typed arrays).

 

* arrays.xml用于资源数组(类型化数组)。

 

* colors.xml for color values

 

* colors.xml用于颜色值

 

* dimens.xml for dimension values.

 

* dimens.xml用于尺寸值。

 

* strings.xml for string values.

 

* strings.xml用于字符串值。

 

* styles.xml for styles.

 

* styles.xml用于样式。

 

See String Resources, Style Resource, and More Resource Types.

 

参见字符串资源,样式资源,以及更多资源类型。

 

* xml/ Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various XML configuration files must be saved here, such as a searchable configuration.

 

* xml/ 任意XML文件,可以在运行时通过调用Resources.getXML()读取。不同的XML配置文件必须保存在这里,诸如一个可搜索配置。

 

-------------------------------

 

-------------------------------

 

Caution: Never save resource files directly inside the res/ directory—it will cause a compiler error.

 

警告:从不直接保存资源文件在res/目录中(注:指非res/子目录?)——它将导致编译器错误。

 

-------------------------------

 

For more information about certain types of resources, see the Resource Types documentation.

 

想获取关于某个类型资源的更多信息,请参见资源类型文档。

 

The resources that you save in the subdirectories defined in table 1 are your "default" resources. That is, these resources define the default design and content for your application. However, different types of Android-powered devices might call for different types of resources. For example, if a device has a larger than normal screen, then you should provide different layout resources that take advantage of the extra screen space. Or, if a device has a different language setting, then you should provide different string resources that translate the text in your user interface. To provide these different resources for different device configurations, you need to provide alternative resources, in addition to your default resources.

 

你保存在定义于表1中的子目录中的资源是你的“默认”资源。就是说,这些资源定义你的应用程序的默认设计和内容。然而,不同类型的基于Android的设备可能需要不同类型的资源。例如,如果一个设备拥有一个大于一般的屏幕,那么你应该放置不同的布局资源以利用额外的屏幕空间。或者,如果一个设备拥有不同的语言设置,那么你应该提供不同的字符串资源以翻译你的用户界面中的文本。为了为不同的设备配置提供这些不同的资源,你需要提供可选资源,附加到你的默认资源。

 

-------------------------------

 

Providing Alternative Resources

 

提供可选资源

 

-------------------------------

 

(图1略:

Android设备A

Android应用

默认资源(注:指向设备A)

可选资源B(注:指向设备B)

Android设备B

 

-------------------------------

 

Figure 1. Two different devices, one using alternative resources.

 

图1. 两个不同的设备,其中一个使用可选资源。

 

Almost every application should provide alternative resources to support specific device configurations. For instance, you should include alternative drawable resources for different screen densities and alternative string resources for different languages. At runtime, Android detects the current device configuration and loads the appropriate resources for your application.

 

几乎每一个应用程序应该提供可选资源以支持特定设备配置。例如,你应该为不同屏幕密度包含可选可绘画对象资源,以及为不同语言提供可选字符串资源。在运行期,Android检测当前设备配置并为你的应用程序加载合适的资源。

 

To specify configuration-specific alternatives for a set of resources:

 

为了为一组资源指定配置特定的候选方案:

 

1. Create a new directory in res/ named in the form <resources_name>-<config_qualifier>.

 

1. 在res/中创建一个新目录,以格式<资源名>-<配置修饰符>的格式命名。

 

* <resources_name> is the directory name of the corresponding default resources (defined in table 1).

 

* <资源名>是对应于默认资源的目录名(定义在表1中)。

 

* <qualifier> is a name that specifies an individual configuration for which these resources are to be used (defined in table 2).

 

* <修饰符>是一个名称,指定这些资源将被用于的一个单独配置(定义在表2中)。

 

You can append more than one <qualifier>. Separate each one with a dash.

 

你可以尾加多于一个<修饰符>。用一个横杠号分隔每一个修饰符。

 

2. Save the respective alternative resources in this new directory. The resource files must be named exactly the same as the default resource files.

 

2. 保存各自的可选资源在这个新目录中。资源文件必须准确地与默认资源文件以相同的名称命名。

 

For example, here are some default and alternative resources:

 

例如,这里有一些默认和可选的资源

 

-------------------------------

 

res/

    drawable/   

        icon.png

        background.png    

    drawable-hdpi/  

        icon.png

        background.png  

 

-------------------------------

 

The hdpi qualifier indicates that the resources in that directory are for devices with a high-density screen. The images in each of these drawable directories are sized for a specific screen density, but the filenames are exactly the same. This way, the resource ID that you use to reference the icon.png or background.png image is always the same, but Android selects the version of each resource that best matches the current device, by comparing the device configuration information with the qualifiers in the alternative resource directory name.

 

hdpi修饰符指示在那个目录中的资源用于带有高密度屏幕的设备。在每个可绘画对象的目录中的图片被缩放以适应特定的屏幕密度,但文件名是准确地相同的。这样,你用于引用icon.png或background.png图片的资源ID总是相同的,但Android选择最佳适配当前设备的每个资源的版本,通过用可选资源目录名中的修饰符比较设备配置信息来做到。

 

Android supports several configuration qualifiers and you can add multiple qualifiers to one directory name, by separating each qualifier with a dash. Table 2 lists the valid configuration qualifiers, in order of precedence—if you use multiple qualifiers for one resource directory, they must be added to the directory name in the order they are listed in the table.

 

Android支持几个配置修饰符,而你可以添加多个修饰符到一个目录名称上,通过用横杠号分隔每个修饰符来做到。表2列举可用的配置修饰符,以优先级顺序排列——如果你对一个资源目录使用多个修饰符,那么它们必须按照它们列举在表中的顺序添加到目录名称上。

 

-------------------------------

 

Note: Some configuration qualifiers were added after Android 1.0, so not all versions of Android support all the qualifiers listed in table 2. New qualifiers indicate the version in which they were added. To avoid any issues, always include a set of default resources for resources that your application uses. For more information, see the section about Providing the Best Device Compatibility with Resources.

 

注意:一些配置修饰符在Android 1.0以后添加,所以不是所有版本的Android支持列举在表2中的所有修饰符。新的修饰符指出它们被添加进的版本。为了避免任何问题,总是为你的应用程序所使用的资源包含一组默认资源。想获取更多信息,请参见关于使用资源提供最佳设备兼容性的章节。

 

-------------------------------

 

Table 2. Configuration qualifier names.

 

表2. 配置修饰符名称。

 

-------------------------------

 

Configuration Qualifier Values Description

 

名称 修饰符值 描述

 

* MCC and MNC

 

* MCC和MNC

 

Examples: 示例:

mcc310

mcc310-mnc004

mcc208-mnc00

etc. 等等

 

The mobile country code (MCC), optionally followed by mobile network code (MNC) from the SIM card in the device. For example, mcc310 is U.S. on any carrier, mcc310-mnc004 is U.S. on Verizon, and mcc208-mnc00 is France on Orange.

 

移动国家代码(MCC),可选地后接来自设备中的SIM卡的移动网络代码(MNC)。例如,mcc310是美国的任意运营商,mcc310-mnc004是美国的Verizon公司,而mcc208-mnc00是法国的Orange公司。

 

If the device uses a radio connection (GSM phone), the MCC comes from the SIM, and the MNC comes from the network to which the device is connected.

 

如果设备使用一个无线连接(GSM手机),那么MCC来自SIM卡,而MNC来自设备连接到的网络。

 

You can also use the MCC alone (for example, to include country-specific legal resources in your application). If you need to specify based on the language only, then use the language and region qualifier instead (discussed next). If you decide to use the MCC and MNC qualifier, you should do so with care and test that it works as expected.

 

你还可以单独使用MCC(例如,为了在你的程序中包含国家特定的合法资源)。如果你只是需要基于语言来指定,那么请改为使用语言和地区修饰符(下面会讨论)。如果你决定使用MCC和MNC修饰符,你应该小心地这样做并且测试它是否如预期那样工作。

 

Also see the configuration fields mcc, and mnc, which indicate the current mobile country code and mobile network code, respectively.

 

另见配置域mcc和mnc,它分别指示当前移动国家代码以及移动网络代码。

 

* Language and region

 

* 语言和地区

 

Examples: 示例:

en

fr

en-rUS

fr-rFR

fr-rCA

etc.  等等

 

The language is defined by a two-letter ISO 639-1 language code, optionally followed by a two letter ISO 3166-1-alpha-2 region code (preceded by lowercase "r").

 

语言通过双字符ISO 639-1语言代号来定义,可选地后接一个双字符ISO 3166-1-alpha-2地区代号(前面是小写的"r")。

 

The codes are not case-sensitive; the r prefix is used to distinguish the region portion. You cannot specify a region alone.

 

代号不是大小写敏感的;r前缀被用于区分地区部分。你不可以只指定地区。

 

This can change during the life of your application if the user changes his or her language in the system settings. See Handling Runtime Changes for information about how this can affect your application during runtime.

 

它可以在你的应用程序生命期间改变,如果用户在系统设置中改变他或她的语言。参见处理运行时改变以获取关于它在运行期期间如何影响你的应用程序的更多信息。

 

See Localization for a complete guide to localizing your application for other languages.

 

参见本地化以获取为其它语言本地化你的应用程序的完整指引。

 

Also see the locale configuration field, which indicates the current locale.

 

另见locale配置域,它指示当前区域。

 

* smallestWidth

 

* 最小宽度

 

sw<N>dp

Examples: 示例:

sw320dp

sw600dp

sw720dp

etc. 等等

 

The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least <N> dps of width available for it UI.

 

屏幕的基本大小,由可用屏幕区域的最短尺寸来指示。特别地,设备的smallestWidth是屏幕可用高度和宽度的最短值(你也可以认为它是屏幕的“最小可能宽度”)。你可以使用这个修饰符以确保,不管屏幕的当前方向是什么,你的应用程序(注:的屏幕?)对于它的用户界面至少有<N> dp的可用宽度。

 

For example, if your layout requires that its smallest dimension of screen area be at least 600 dp at all times, then you can use this qualifer to create the layout resources, res/layout-sw600dp/. The system will use these resources only when the smallest dimension of available screen is at least 600dp, regardless of whether the 600dp side is the user-perceived height or width. The smallestWidth is a fixed screen size characteristic of the device; the device's smallestWidth does not change when the screen's orientation changes.

 

例如,如果你的布局需要它的屏幕区域的最小尺寸在所有时候至少为600 dp,那么你可以使用这个修饰符去创建布局资源,res/layout-sw600dp/。系统将使用这些资源仅当可用屏幕的最小尺寸至少为600dp,不管600dp的方向是用户可见的长还是宽。smallestWidth是设备的固定屏幕大小特性;设备的smallestWidth在屏幕的方向改变时不改变。

 

The smallestWidth of a device takes into account screen decorations and system UI. For example, if the device has some persistent UI elements on the screen that account for space along the axis of the smallestWidth, the system declares the smallestWidth to be smaller than the actual screen size, because those are screen pixels not available for your UI. Thus, the value you use should be the actual smallest dimension required by your layout (usually, this value is the "smallest width" that your layout supports, regardless of the screen's current orientation).

 

设备的smallestWidth考虑屏幕装饰和系统用户界面。例如,如果设备在屏幕拥有一些持久的用户界面元素,它们考虑沿着smallestWidth的轴线的空间,那么系统声明的smallestWidth比实际屏幕大小要小,因为那些是对于你的用户界面不可用的屏幕像素。因此,你使用的值应该是你的布局需要的实际最小尺寸(通常,这个值是你的布局支持的“最小宽度”,不管屏幕的当前方向是什么)。

 

Some values you might use here for common screen sizes:

 

这里有一些你可能用于一般屏幕大小的值:

 

* 320, for devices with screen configurations such as:

 

* 320,用于带有如下屏幕配置的设备:

 

* 240x320 ldpi (QVGA handset)  

* 240x320 ldpi(QVGA手机)

 

* 320x480 mdpi (handset)  

* 320x480 mdpi(手机)

 

* 480x800 hdpi (high density handset)  

* 480x800 hdpi(高密度手机)

 

* 480, for screens such as 480x800 mdpi (tablet/handset).  

* 480,用于屏幕诸如480x800 mdpi(平板/手机)

 

* 600, for screens such as 600x1024 mdpi (7" tablet). 

* 600,用于屏幕诸如600x1024 mdpi(7"平板)

 

* 720, for screens such as 720x1280 mdpi (10" tablet). 

* 720,用于屏幕诸如720x1280 mdpi(10"平板)

 

When your application provides multiple resource directories with different values for the smallestWidth qualifier, the system uses the one closest to (without exceeding) the device's smallestWidth.

 

当你的应用程序用smallestWidth修饰符的不同值提供多个资源目录时,系统使用最接近(但不超过)设备的smallestWidth的那个目录。

 

Added in API level 13.

 

API级别13中新增。

 

Also see the android:requiresSmallestWidthDp attribute, which declares the minimum smallestWidth with which your application is compatible, and the smallestScreenWidthDp configuration field, which holds the device's smallestWidth value.

 

另见android:requiresSmallestWidthDp属性,它声明你的应用程序兼容的最小smallestWidth,以及smallestScreenWidthDp配置域,它持有设备的smallestWidth值

 

For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide.

 

想获取关于设计不同屏幕和使用这个修饰符的更多信息,请参见支持多屏幕开发者指南。

 

* Available width

 

* 可用的宽度 

 

w<N>dp

Examples: 示例:

w720dp

w1024dp

etc. 等等

 

Specifies a minimum available screen width, in dp units at which the resource should be used—defined by the <N> value. This configuration value will change when the orientation changes between landscape and portrait to match the current actual width.

 

指定资源应该使用的最小可用屏幕宽度,以dp为单位——通过<N>值定义。这个配置值将在方向在宽屏和竖屏之间改变时改变,以匹配当前实际宽度。

 

When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen width. The value here takes into account screen decorations, so if the device has some persistent UI elements on the left or right edge of the display, it uses a value for the width that is smaller than the real screen size, accounting for these UI elements and reducing the application's available space.

 

当你的用于程序用这个配置的不同值提供多个资源目录时,系统使用其中最接近(不超过)设备当前屏幕宽度的那个目录。这里的值考虑屏幕装饰,所以如果设备在显示的左或右边沿拥有一些持久用户界面元素,那么它用于宽度的值小于实际屏幕大小,考虑这些用户界面元素并降低应用程序的可用空间。

 

Added in API level 13.

 

API级别13中新增。

 

Also see the screenWidthDp configuration field, which holds the current screen width.

 

另见screenWidthDp配置域,它持有当前屏幕宽度。

 

For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide.

 

想获取关于设计不同屏幕和使用这个修饰符的更多信息,请参见支持多屏幕开发者指引。

 

* Available height

 

* 可用的高度 

 

h<N>dp

Examples: 示例:

h720dp

h1024dp

etc. 等等。

 

Specifies a minimum available screen height, in "dp" units at which the resource should be used—defined by the <N> value. This configuration value will change when the orientation changes between landscape and portrait to match the current actual height.

 

指定资源应该使用的最小可用屏幕高度,以dp为单位——通过<N>值定义。这个配置值将在方向在宽屏和竖屏之间改变时改变,以匹配当前的实际高度。

 

When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen height. The value here takes into account screen decorations, so if the device has some persistent UI elements on the top or bottom edge of the display, it uses a value for the height that is smaller than the real screen size, accounting for these UI elements and reducing the application's available space. Screen decorations that are not fixed (such as a phone status bar that can be hidden when full screen) are not accounted for here, nor are window decorations like the title bar or action bar, so applications must be prepared to deal with a somewhat smaller space than they specify.

 

当你的应用程序用这个配置的不同值提供多个资源目录时,系统使用最接近(不超过)设备当前屏幕高度的那一个。这里的值考虑了屏幕装饰,所以如果设备有一些持久的用户界面元素在显示屏的上方和底部边沿,那么它使用小于实际屏幕大小的高度作为值,考虑这些用户界面元素并减少应用程序的可用空间。不固定的屏幕装饰(诸如可以在全屏时隐藏的电话状态栏)在这里不考虑,同样窗口装饰如标题栏或动作栏也不考虑,所以应用程序必须准备好应对一些比它们指定的稍微小的空间。

 

Added in API level 13.

 

API级别13中新增。

 

Also see the screenHeightDp configuration field, which holds the current screen width.

 

另见screenHeightDp配置域,它持有当前屏幕宽度。

 

For more information about designing for different screens and using this qualifier, see the Supporting Multiple Screens developer guide.

 

想获取关于设计不同屏幕和使用这个修饰符的更多信息,请参见支持多屏幕开发者指引。

 

* Screen size

 

* 屏幕大小

 

small 小

normal 普通

large 大

xlarge 超大

 

small: Screens that are of similar size to a low-density QVGA screen. The minimum layout size for a small screen is approximately 320x426 dp units. Examples are QVGA low density and VGA high density.

 

小:类似低密度QVGA屏幕大小的屏幕。一个小屏幕的最小布局大小大概为320x426 dp单位。例如QVGA低密度和VGA高密度。

 

normal: Screens that are of similar size to a medium-density HVGA screen. The minimum layout size for a normal screen is approximately 320x470 dp units. Examples of such screens a WQVGA low density, HVGA medium density, WVGA high density.

 

普通:类似中等密度HVGA屏幕大小的屏幕。一个普通屏幕的最小布局大小大概为320x470 dp单位。这种屏幕例如是WQVGA低密度,HVGA中等密度,WVGA高密度。

 

large: Screens that are of similar size to a medium-density VGA screen. The minimum layout size for a large screen is approximately 480x640 dp units. Examples are VGA and WVGA medium density screens.

 

大:类似一个中等密度VGA屏幕大小的屏幕。一个大屏幕的最小布局大小大概为480x640 dp单位。例如有VGA和WVGA中等密度屏幕。

 

xlarge: Screens that are considerably larger than the traditional medium-density HVGA screen. The minimum layout size for an xlarge screen is approximately 720x960 dp units. In most cases, devices with extra large screens would be too large to carry in a pocket and would most likely be tablet-style devices. Added in API level 9.

 

超大:屏幕比传统的中等密度HVGA屏幕还要大。一个超大屏幕的最小布局大小大概为720x960 dp单位。在大多数情况下,带有超大设备的设备太大而不能放进口袋,并且最有可能是平板风格的设备。在API级别9中新增。

 

-------------------------------

 

Note: Using a size qualifier does not imply that the resources are only for screens of that size. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match.

 

注意:使用一个大小修饰符不暗示资源只用于那个大小的屏幕。如果你不用较佳匹配当前设备配置的修饰符提供可选资源,那么系统可能使用最佳匹配的资源而不管是哪一个。

 

-------------------------------

 

-------------------------------

 

Caution: If all your resources use a size qualifier that is larger than the current screen, the system will not use them and your application will crash at runtime (for example, if all layout resources are tagged with the xlarge qualifier, but the device is a normal-size screen).

 

警告:如果你的所有资源使用一个大于当前屏幕的大小修饰符,那么系统将不使用它们,而且你的应用程序将在运行期崩溃(例如,如果所有布局资源用xlarge修饰符标注,但设备是普通大小的屏幕)。

 

-------------------------------

 

Added in API level 4.

 

API级别4中新增。

 

See Supporting Multiple Screens for more information.

 

参见支持多屏幕以获取更多信息。

 

Also see the screenLayout configuration field, which indicates whether the screen is small, normal, or large.

 

另见screenLayout配置域,它指示屏幕是否为小、一般,或大。

 

* Screen aspect

 

* 屏幕切面

 

long 长

notlong 非长

 

long: Long screens, such as WQVGA, WVGA, FWVGA 

 

长:长屏幕,诸如WQVGA,WVGA,FWVGA

 

notlong: Not long screens, such as QVGA, HVGA, and VGA

 

非长:不是宽屏,诸如QVGA,HVGA,和VGA

 

Added in API level 4.

 

API级别4中新增。

 

This is based purely on the aspect ratio of the screen (a "long" screen is wider). This is not related to the screen orientation.

 

这纯粹是基于屏幕的切面比例(一个“长”屏幕较宽)。它与屏幕方向无关。

 

Also see the screenLayout configuration field, which indicates whether the screen is long.

 

另见screenLayout配置域,它指示屏幕是否为长的。

 

* Screen orientation  

 

* 屏幕方向

 

port

land

 

port: Device is in portrait orientation (vertical) 

 

port:设备在竖屏方向(垂直)

 

land: Device is in landscape orientation (horizontal)

 

land:设备在宽屏方向(水平)

 

This can change during the life of your application if the user rotates the screen. See Handling Runtime Changes for information about how this affects your application during runtime.

 

它可以在你的应用程序的生命期期间改变,如果用户旋转屏幕。参见处理运行时改变以获取关于它如何在运行期期间影响你的应用程序的信息。

 

Also see the orientation configuration field, which indicates the current device orientation.

 

另见orientation配置域,它指示当前设备的方向。

 

* Dock mode

 

* 停靠模式

 

car

desk

 

car: Device is in a car dock

 

car:设备处于汽车停靠

 

desk: Device is in a desk dock

 

desk:设备处于桌面停靠

 

Added in API level 8.

 

API级别8中新增。

 

This can change during the life of your application if the user places the device in a dock. You can enable or disable this mode using UiModeManager. See Handling Runtime Changes for information about how this affects your application during runtime.

 

它可以在你的应用程序的生命期间改变,如果用户放置设备在一个底座中。你可以用UiModeManager使能和屏蔽此模式。参见处理运行时改变以获取它如何在运行期影响你的应用程序的信息。

 

* Night mode

 

* 夜间模式 

 

night

notnight

 

night: Night time

 

night:夜间时间

 

notnight: Day time

 

notnight:日间时间

 

Added in API level 8.

 

API级别8中新增。

 

This can change during the life of your application if night mode is left in auto mode (default), in which case the mode changes based on the time of day. You can enable or disable this mode using UiModeManager. See Handling Runtime Changes for information about how this affects your application during runtime.

 

它可以在你的应用程序的生命期间改变,如果夜间模式停留在自动模式(默认),在这种情况下模式基于一天的时间来改变。你可以使用UiModeManager使能或关闭这个模式。参见处理运行期改变以获取关于它如何在运行期影响你的应用程序的信息。

 

* Screen pixel density (dpi)

* 屏幕像素密度(dpi) 

 

ldpi

mdpi

hdpi

xhdpi

nodpi

tvdpi

 

ldpi: Low-density screens; approximately 120dpi.

 

ldpi:低密度屏幕;大概为120dpi。

 

mdpi: Medium-density (on traditional HVGA) screens; approximately 160dpi.

 

mdpi:中密度(在传统HVGA上)屏幕;大概为160dpi

 

hdpi: High-density screens; approximately 240dpi.

 

hdpi:高密度屏幕;大概为240dpi。

 

xhdpi: Extra high-density screens; approximately 320dpi. Added in API Level 8

 

xhdpi:超高密度屏幕;大概为320dpi。在API级别8中新增

 

nodpi: This can be used for bitmap resources that you do not want to be scaled to match the device density.

 

nodpi:这可以用于你不希望缩放以匹配设备密度的位图资源。

 

tvdpi: Screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a "primary" density group. It is mostly intended for televisions and most apps shouldn't need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. This qualifier was introduced with API level 13.

 

tvdpi:屏幕大概在mdpi和hdpi之间;大概为213dpi。它不被认为是一个“主”密度分组。它几乎倾向于电视而大多数应用不会需要它——提供mdpi和hdpi资源对于大多数应用来说是足够的,而系统将合理地缩放它们。此修饰符在API级别13中引入。

 

There is a 3:4:6:8 scaling ratio between the four primary densities (ignoring the tvdpi density). So, a 9x9 bitmap in ldpi is 12x12 in mdpi, 18x18 in hdpi and 24x24 in xhdpi.

 

在这四种主密度之间存在一种3:4:6:8的缩放比例(忽略tvdpi密度)。所以,在ldpi中的9x9位图在mdpi中是12x12,在hdpi中是18x18,而在xhdpi中为24x24。

 

If you decide that your image resources don't look good enough on a television or other certain devices and want to try tvdpi resources, the scaling factor is 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi.

 

如果你认定你的位图资源在电视上或者其它某种设备上看起来不够好,而希望尝试tvdpi资源,缩放因子是1.33*mdpi。例如,一个对于mdpi屏幕为100px x 100px的图片应该对于tvdpi是133px x 133px。

 

-------------------------------

 

Note: Using a density qualifier does not imply that the resources are only for screens of that density. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match.

 

注意:使用一个密度修饰符并不暗示资源只用于那种密度的屏幕。如果你不使用更好地匹配当前设备配置的修饰符提供可选资源,那么系统可能随便地使用最佳匹配的资源。

 

-------------------------------

 

See Supporting Multiple Screens for more information about how to handle different screen densities and how Android might scale your bitmaps to fit the current density.

 

参见支持多屏幕以获取关于如何处理不同屏幕密度和Android可能如何缩放你的位图以适应当前密度的更多信息。

 

* Touchscreen type

 

* 触摸屏类型 

 

notouch

stylus

finger

 

notouch: Device does not have a touchscreen.

 

notouch:设备没有触摸屏。

 

stylus: Device has a resistive touchscreen that's suited for use with a stylus.

 

stylus:设备有一个电阻式触摸屏,它适合用手写笔来使用。

 

finger: Device has a touchscreen.

 

finger:设备有一个触摸屏。

 

Also see the touchscreen configuration field, which indicates the type of touchscreen on the device.

 

另见touchscreen配置域,它指示设备上触摸屏类型。

 

* Keyboard availability

 

* 键盘可用性

 

keysexposed

keyshidden

keyssoft

 

keysexposed: Device has a keyboard available. If the device has a software keyboard enabled (which is likely), this may be used even when the hardware keyboard is not exposed to the user, even if the device has no hardware keyboard. If no software keyboard is provided or it's disabled, then this is only used when a hardware keyboard is exposed.

 

keysexposed:设备有一个可用键盘。如果设备有一个使能的软键盘(它可能是),甚至在硬键盘没有暴露给用户时它也可能被使用,即便设备没有硬键盘。如果没有提供软键盘或它被屏蔽,那么它只能在硬键盘暴露时才被使用。

 

keyshidden: Device has a hardware keyboard available but it is hidden and the device does not have a software keyboard enabled.

 

keyshidden:设备有一个可用的硬键盘但它被隐藏,而且设备没有开启软键盘。

 

keyssoft: Device has a software keyboard enabled, whether it's visible or not.

 

keyssoft:设备有一个使能的软键盘,不管它是否可见。

 

If you provide keysexposed resources, but not keyssoft resources, the system uses the keysexposed resources regardless of whether a keyboard is visible, as long as the system has a software keyboard enabled.

 

如果你提供keysexposed资源,都没有keyssoft资源,那么系统使用keysexposed资源而不管键盘是否是可见的,只要系统有一个开启的软键盘。

 

This can change during the life of your application if the user opens a hardware keyboard. See Handling Runtime Changes for information about how this affects your application during runtime.

 

这可能在你的应用程序生命期期间改变,如果用户打开一个硬键盘。参见处理运行时改变以获取关于它如何在运行时影响你的应用程序的信息。

 

Also see the configuration fields hardKeyboardHidden and keyboardHidden, which indicate the visibility of a hardware keyboard and and the visibility of any kind of keyboard (including software), respectively.

 

另见配置域hardKeyboardHidden和keyboardHidden,它指示硬键盘的可见性以及任意类型键盘(包括软件)相应的可见性。

 

* Primary text input method

 

* 主文本输入方法

 

nokeys

qwerty

12key

 

nokeys: Device has no hardware keys for text input.

 

nokeys:设备没有用于文本输入的硬按键。

 

qwerty: Device has a hardware qwerty keyboard, whether it's visible to the user or not.

 

qwerty:设备有一个硬件全键盘,它是否对用户可见。

 

12key: Device has a hardware 12-key keyboard, whether it's visible to the user or not.

 

12key:设备有一个硬件12键键盘,它是否对用户可见。

 

Also see the keyboard configuration field, which indicates the primary text input method available.

 

另见键盘配置域,它指示可用的主文本输入方法

 

* Navigation key availability

 

* 导航键可用性

 

navexposed

navhidden

 

navexposed: Navigation keys are available to the user.

 

navexposed:导航键对于用户可用。

 

navhidden: Navigation keys are not available (such as behind a closed lid).

 

navhidden:导航键不可用(诸如在一个关闭的盖子后面)。

 

This can change during the life of your application if the user reveals the navigation keys. See Handling Runtime Changes for information about how this affects your application during runtime.

 

这可能在你的应用程序的生命期期间改变,如果用户展开导航键。参见处理运行时改变以获取关于它如何在运行时影响你的应用程序的信息。

 

Also see the navigationHidden configuration field, which indicates whether navigation keys are hidden.

 

另见navigationHidden配置域,它指示导航键是否被隐藏。

 

* Primary non-touch navigation method

 

* 主非触碰导航方法

 

nonav

dpad

trackball

wheel

 

nonav: Device has no navigation facility other than using the touchscreen.

 

nonav:设备没有不是触摸屏的导航设施

 

dpad: Device has a directional-pad (d-pad) for navigation.

 

dpad:设备有一个方向面板(d-pad)用于导航。

 

trackball: Device has a trackball for navigation.

 

trackball:设备有一个轨迹球用于导航。

 

wheel: Device has a directional wheel(s) for navigation (uncommon).

 

滚轮:设备有一个方向滚轮用于导航(罕见)。

 

Also see the navigation configuration field, which indicates the type of navigation method available.

 

另见navigation配置域,它指示可用导航方法的类型。

 

* Platform Version (API level)

 

* 平台版本(API级别)

 

Examples:  示例:

v3

v4

v7

etc. 等等

 

The API level supported by the device. For example, v1 for API level 1 (devices with Android 1.0 or higher) and v4 for API level 4 (devices with Android 1.6 or higher). See the Android API levels document for more information about these values.

 

设备支持的API级别。例如,v1用于API级别1(带有Android 1.0或更高的设备)而v4用于API级别4(带有Android 1.6或更高的设备)。另见Android API级别文档以获取关于这些值的更多信息。

 

-------------------------------

 

Caution: Android 1.5 and 1.6 only match resources with this qualifier when it exactly matches the platform version. See the section below about Known Issues for more information.

 

警告:Android 1.5和1.6只匹配带有这个修饰符的资源,当它精确地匹配平台版本时。见下面关于已知问题的章节以获取更多信息。

 

-------------------------------

 

Qualifier name rules

 

修饰符命名规则

 

Here are some rules about using configuration qualifier names:

 

这里有一些关于如何使用配置修饰符名称的规则:

 

* You can specify multiple qualifiers for a single set of resources, separated by dashes. For example, drawable-en-rUS-land applies to US-English devices in landscape orientation.

 

* 你可以对单组资源指定多个修饰符,用横杠号分隔。例如,drawable-en-rUS-land应用于宽屏方向的美国-英语设备。

 

* The qualifiers must be in the order listed in table 2. For example:

 

 修饰符必须按照表2中列举的顺序。例如:

 

* Wrong: drawable-hdpi-port/

 

* 错误:drawable-hdpi-port/

 

* Correct: drawable-port-hdpi/

 

* 正确:drawable-port-hdpi/

 

* Alternative resource directories cannot be nested. For example, you cannot have res/drawable/drawable-en/.

 

* 可选资源目录不可以嵌套。例如,你不可以拥有res/drawable/drawable-en/。

 

* Values are case-insensitive. The resource compiler converts directory names to lower case before processing to avoid problems on case-insensitive file systems. Any capitalization in the names is only to benefit readability.

 

* 值是大小写不敏感的。资源编译器在处理之前转换目录名称为小写以避免在大小写不敏感的文件系统上的问题。名称中的任意大写只是为了便于可读。

 

* Only one value for each qualifier type is supported. For example, if you want to use the same drawable files for Spain and France, you cannot have a directory named drawable-rES-rFR/. Instead you need two resource directories, such as drawable-rES/ and drawable-rFR/, which contain the appropriate files. However, you are not required to actually duplicate the same files in both locations. Instead, you can create an alias to a resource. See Creating alias resources below.

 

* 对于每个修饰符类型只支持一个值。例如,如果你希望对于西班牙语和法语使用相同的可绘画文件,你不可以拥有一个命名为drawable-rES-rFR/的目录。取而代之,你需要两个资源目录,诸如drawable-rES/和drawable-rFR/,它包含合适的文件。然而,你实际上不需要都复制相同文件到两个位置。取而代之,你可以创建指向资源的别名。参见下面的创建别名资源。

 

After you save alternative resources into directories named with these qualifiers, Android automatically applies the resources in your application based on the current device configuration. Each time a resource is requested, Android checks for alternative resource directories that contain the requested resource file, then finds the best-matching resource (discussed below). If there are no alternative resources that match a particular device configuration, then Android uses the corresponding default resources (the set of resources for a particular resource type that does not include a configuration qualifier).

 

在你把可选资源保存进用这些修饰符命名的目录时,Android在你的应用程序中自动地应用资源,基于当前的设备配置。每当需要一个资源时,Android检查包含请求资源文件的可选资源目录,然后找到最佳匹配资源(下面会讨论)。如果没有可选资源匹配特定设备配置,那么Android使用相应的默认资源(用于特定资源类型的资源组,它不包含配置修饰符)。

 

Creating alias resources

 

创建别名资源

 

When you have a resource that you'd like to use for more than one device configuration (but do not want to provide as a default resource), you do not need to put the same resource in more than one alternative resource directory. Instead, you can (in some cases) create an alternative resource that acts as an alias for a resource saved in your default resource directory.

 

当你有一个你希望用于多于一个设备配置的资源(但不希望提供为一个默认资源),你不需要放置相同资源在多于一个可选资源目录中。取而代之,你可以(在一些情况下)创建一个可选资源,其行为如同一个保存在默认资源目录中的资源的别名。

 

-------------------------------

 

Note: Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.

 

注意:不是所有资源提供你可以创建一个指向另一个资源别名的机制。特别地,动画、菜单、原始,以及其它在xml/目录中的非特定资源不提供此特性。

 

-------------------------------

 

For example, imagine you have an application icon, icon.png, and need unique version of it for different locales. However, two locales, English-Canadian and French-Canadian, need to use the same version. You might assume that you need to copy the same image into the resource directory for both English-Canadian and French-Canadian, but it's not true. Instead, you can save the image that's used for both as icon_ca.png (any name other than icon.png) and put it in the default res/drawable/ directory. Then create an icon.xml file in res/drawable-en-rCA/ and res/drawable-fr-rCA/ that refers to the icon_ca.png resource using the <bitmap> element. This allows you to store just one version of the PNG file and two small XML files that point to it. (An example XML file is shown below.)

 

例如,想象你有一个应用程序图标,icon.png,并且需要它的唯一版本用于不同区域。然而,两个区域,英语-加拿大和法语-加拿大,需要使用相同版本。你可能假设你都需要复制相同的图片进用于英语-加拿大和法语-加拿大资源目录,但这并不对。相反,你可以用于这两者的图片保存为icon_ca.png(任意不同于icon.png的名称)并把它放在默认的res/drawable/目录中。然后创建一个icon.xml文件在res/drawable-en-rCA/和res/drawable-fr-rCA/中,它使用<bitmap>元素引用icon_ca.png资源。这允许你只保存一个版本的PNG文件和两个指向它的小型XML文件。(下面展示一个示例XML文件。)

 

Drawable

 

可绘画

 

To create an alias to an existing drawable, use the <bitmap> element. For example:

 

为了创建一个现存可绘画对象的别名,请使用<bitmap>元素。例如:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"

    android:src="@drawable/icon_ca" />

 

-------------------------------

 

If you save this file as icon.xml (in an alternative resource directory, such as res/drawable-en-rCA/), it is compiled into a resource that you can reference as R.drawable.icon, but is actually an alias for the R.drawable.icon_ca resource (which is saved in res/drawable/).

 

如果你保存这个文件为icon.xml(在一个可选资源目录中,诸如res/drawable-en-rCA/),那么它被编译进你可以引用为R.drawable.icon的一个资源中,但实际上是R.drawable.icon_ca资源的一个别名(它被保存在res/drawable/中)。

 

Layout

 

布局

 

To create an alias to an existing layout, use the <include> element, wrapped in a <merge>. For example:

 

为了创建一个现存布局的别名,使用<include>元素,包围在<merge>中。例如:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<merge>

    <include layout="@layout/main_ltr"/>

</merge>

 

-------------------------------

 

If you save this file as main.xml, it is compiled into a resource you can reference as R.layout.main, but is actually an alias for the R.layout.main_ltr resource.

 

如果你保存这个文件为main.xml,那么它被编译进你可以引用为R.layout.main的资源,但实际上是R.layout.main_ltr资源的一个别名。

 

Strings and other simple values

 

字符串和其它简单值

 

To create an alias to an existing string, simply use the resource ID of the desired string as the value for the new string. For example:

 

为了对一个现存字符串创建一个别名,简单地使用期待的字符串的资源ID作为新字符串的值。例如:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello</string>

    <string name="hi">@string/hello</string>

</resources>

 

-------------------------------

 

The R.string.hi resource is now an alias for the R.string.hello.

 

R.string.hi资源现在是R.string.hello的别名。

 

Other simple values work the same way. For example, a color:

 

其它简单值以相同的方式工作,例如,一个颜色:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <color name="yellow">#f00</color>

    <color name="highlight">@color/red</color>

</resources>

 

-------------------------------

 

-------------------------------

 

Providing the Best Device Compatibility with Resources

 

使用资源提供最佳设备兼容性

 

In order for your application to support multiple device configurations, it's very important that you always provide default resources for each type of resource that your application uses.

 

为了让你的应用程序支持多屏幕配置,最重要的是你总是为你的应用程序使用的每个类型的资源提供默认资源。

 

For example, if your application supports several languages, always include a values/ directory (in which your strings are saved) without a language and region qualifier. If you instead put all your string files in directories that have a language and region qualifier, then your application will crash when run on a device set to a language that your strings do not support. But, as long as you provide default values/ resources, then your application will run properly (even if the user doesn't understand that language—it's better than crashing).

 

例如,如果你的应用程序支持几种语言,那么总是包含一个不带语言和地区修饰符的values/目录(你的字符串保存其中)。如果你相反地放置所有你的字符串文件在拥有语言和地区修饰符的目录中,那么你的应用程序将在运行于设置为你的字符串不支持的语言的设备上时崩溃。但是,只要你提供默认的values/资源,那么你的应用程序将正确地运行(即便用户不理解那种语言——这比崩溃要好)。

 

Likewise, if you provide different layout resources based on the screen orientation, you should pick one orientation as your default. For example, instead of providing layout resources in layout-land/ for landscape and layout-port/ for portrait, leave one as the default, such as layout/ for landscape and layout-port/ for portrait.

 

同样,如果你基于屏幕方向提供不同的布局资源,那么你应该选取一个方向作为你的默认方向。例如,不是在layout-land/中为宽屏提供布局资源以及在layout-port/中为竖屏提供布局资源,而是保留其中一个作为默认,诸如layout/用于宽屏而layout-port/用于竖屏。

 

Providing default resources is important not only because your application might run on a configuration you had not anticipated, but also because new versions of Android sometimes add configuration qualifiers that older versions do not support. If you use a new resource qualifier, but maintain code compatibility with older versions of Android, then when an older version of Android runs your application, it will crash if you do not provide default resources, because it cannot use the resources named with the new qualifier. For example, if your minSdkVersion is set to 4, and you qualify all of your drawable resources using night mode (night or notnight, which were added in API Level 8), then an API level 4 device cannot access your drawable resources and will crash. In this case, you probably want notnight to be your default resources, so you should exclude that qualifier so your drawable resources are in either drawable/ or drawable-night/.

 

提供默认资源是重要的,不仅仅因为你的应用程序可能运行在一个你不曾预计的配置上,还因为新版本的Android有时会添加较旧版本不支持的配置修饰符。如果你使用一个新的资源修饰符,但维护对较旧版本Android的代码兼容性,那么当一个较旧版本的Android运行你的应用程序时,它将崩溃,如果你不提供默认资源,因为它不允许使用带有新修饰符命名的资源。例如,如果你的minSdkVersion设置为4,而且你限定你的所有可绘画资源使用夜间模式(夜间或日间,它在API级别8中新增),那么一个API级别4的设备不允许访问你的可绘画资源并且将崩溃。在这种情况下,你很可能希望日间是你的默认资源,所以你应该不包含那个修饰符,所以你的可绘画资源是在drawable/或drawable-night/中。

 

So, in order to provide the best device compatibility, always provide default resources for the resources your application needs to perform properly. Then create alternative resources for specific device configurations using the configuration qualifiers.

 

所以,为了提供最佳设备兼容性,总是为你的应用程序所需要以正确地执行的资源提供默认资源。然后使用配置修饰符为特定设备配置创建可选资源。

 

There is one exception to this rule: If your application's minSdkVersion is 4 or greater, you do not need default drawable resources when you provide alternative drawable resources with the screen density qualifier. Even without default drawable resources, Android can find the best match among the alternative screen densities and scale the bitmaps as necessary. However, for the best experience on all types of devices, you should provide alternative drawables for all three types of density. If your minSdkVersion is less than 4 (Android 1.5 or lower), be aware that the screen size, density, and aspect qualifiers are not supported on Android 1.5 or lower, so you might need to perform additional compatibility for these versions.

 

这个规则有一个例外:如果你的拥有程序的minSdkVersion是4或更高,你不需要默认可绘画资源,当你用屏幕密度修饰符提供可选可绘画资源时。即使没有默认可绘画资源,Android仍可以在可选屏幕密度之间找到最佳适配并且在需要时缩放位图。然而,为了达到所有类型设备上的最佳体验,你应该为所有三种类型的密度提供可选可绘画对象。如果你的minSdkVersion小于4(Android 1.5或更低),请意识到屏幕大小,密度,以及切面(注:纵横比)修饰符在Android 1.5或更低的版本上不支持,所以你可能需要为这些版本执行(注:提供)附加的兼容性。

 

Providing screen resource compatibility for Android 1.5

 

为Android 1.5提供屏幕资源兼容性

 

Android 1.5 (and lower) does not support the following configuration qualifers:

 

Android 1.5(以及更低)不支持以下配置修饰符:

 

* Density

 

* 密度

 

ldpi, mdpi, ldpi, and nodpi

 

* Screen size 

 

* 屏幕大小

 

small, normal, and large 

小、普通和大

 

* Screen aspect

 

* 屏幕切面

 

long and notlong 

长和非长

 

These configuration qualifiers were introduced in Android 1.6, so Android 1.5 (API level 3) and lower does not support them. If you use these configuration qualifiers and do not provide corresponding default resources, then an Android 1.5 device might use any one of the resource directories named with the above screen configuration qualifiers, because it ignores these qualifiers and uses whichever otherwise-matching drawable resource it finds first.

 

这些配置修饰符在Android 1.6中引入,所以Android 1.5(API级别3)以及更低不支持它们。如果你使用这些配置修饰符并且不提供相应的默认资源,那么一个Android 1.5设备可能使用其中任意一个命名带有上面的屏幕配置修饰符的资源目录,因为它忽略这些修饰符并且随意地使用它最先找到的其它方面匹配的可绘画资源。

 

For example, if your application supports Android 1.5 and includes drawable resources for each density type (drawable-ldpi/, drawable-mdpi/, and drawable-ldpi/), and does not include default drawable resources (drawable/), then an Android 1.5 will use drawables from any one of the alternative resource directories, which can result in a user interface that's less than ideal.

 

例如,如果你的应用程序支持Android 1.5并且包含用于每个密度类型的可绘画资源(drawable-ldpi/,drawable-mdpi/,以及drawable-ldpi/),并且不包含默认可绘画资源(drawable/),那么一个Android 1.5设备将从任意一个可选资源目录中使用可绘画对象,这可以导致一个不太理想的用户界面。

 

So, to provide compatibility with Android 1.5 (and lower) when using the screen configuration qualifiers:

 

所以,在使用屏幕配置修饰符时为了提供Android 1.5(和更低)的兼容性:

 

1. Provide default resources that are for medium-density, normal, and notlong screens.

 

1. 提供用于中等密度、普通和非长屏幕的默认资源。

 

Because all Android 1.5 devices have medium-density, normal, not-long screens, you can place these kinds of resources in the corresponding default resource directory. For example, put all medium density drawable resources in drawable/ (instead of drawable-mdpi/), put normal size resources in the corresponding default resource directory, and notlong resources in the corresponding default resource directory.

 

因为所有Android 1.5设备拥有中等密度、普通、非长屏幕,所以你可以放置这些类型的资源在相应的默认资源目录中。例如,放置所有中等密度的可绘画资源在drawable/(而非drawable-mdpi/),放置普通大小的资源在相应的默认资源目录,以及非长资源在相应的默认资源目录中。

 

2. Ensure that your SDK Tools version is r6 or greater.

 

2. 确保你的SDK工具版本是r6或更高。

 

You need SDK Tools, Revision 6 (or greater), because it includes a new packaging tool that automatically applies an appropriate version qualifier to any resource directory named with a qualifier that does not exist in Android 1.0. For example, because the density qualifier was introduced in Android 1.6 (API level 4), when the packaging tool encounters a resource directory using the density qualifier, it adds v4 to the directory name to ensure that older versions do not use those resources (only API level 4 and higher support that qualifier). Thus, by putting your medium-density resources in a directory without the mdpi qualifier, they are still accessible by Android 1.5, and any device that supports the density qualifer and has a medium-density screen also uses the default resources (which are mdpi) because they are the best match for the device (instead of using the ldpi or hdpi resources).

 

你需要SDK工具,修订版6(或更高),因为它包含一个新的打包工具,自动地应用一个合适的版本修饰符到任意带有Android 1.0中不存在的修饰符所命名的资源目录。例如,因为密度修饰符在Android 1.6(API级别4)中引入,当打包工具遇到一个使用密度修饰符的资源目录时,它添加v4到目录名以确保较旧的版本不使用那些资源(只有API级别4和更高版本支持那个修饰符)。这样,通过把你的中等密度资源放到不带mdpi修饰符的目录中,他们仍然可以被Android 1.5访问,而且任意支持密度修饰符并且拥有中等密度屏幕的设备也使用默认资源(它为mdpi)因为它们最佳适配该设备(而不使用ldpi或hdpi资源)。

 

-------------------------------

 

Note: Later versions of Android, such as API level 8, introduce other configuration qualifiers that older version do not support. To provide the best compatibility, you should always include a set of default resources for each type of resource that your application uses, as discussed above to provide the best device compatibility.

 

注意:较新版本的Android,诸如API级别8,引入其它旧版本不支持的配置修饰符,为了提供最佳兼容性,你应该总是为你的应用程序使用的每类资源包含一组默认资源,如同上面讨论的那样以提供最佳的设备兼容性。

 

-------------------------------

 

-------------------------------

 

How Android Finds the Best-matching Resource

 

Android如何找到最佳匹配资源

 

When you request a resource for which you provide alternatives, Android selects which alternative resource to use at runtime, depending on the current device configuration. To demonstrate how Android selects an alternative resource, assume the following drawable directories each contain different versions of the same images:

 

当你请求一个你提供的那个可选资源时,Android在运行期选择使用哪个可选资源,依赖于当前的设备配置。为了演示Android如何选择一个可选资源,请假设以下可绘画目录各自包含相同图片的不同版本:

 

-------------------------------

 

drawable/

drawable-en/

drawable-fr-rCA/

drawable-en-port/

drawable-en-notouch-12key/

drawable-port-ldpi/

drawable-port-notouch-12key/

 

-------------------------------

 

And assume the following is the device configuration:

 

并且假设以下是设备的配置:

 

-------------------------------

 

Locale = en-GB 

Screen orientation = port 

Screen pixel density = hdpi 

Touchscreen type = notouch 

Primary text input method = 12key

 

-------------------------------

 

By comparing the device configuration to the available alternative resources, Android selects drawables from drawable-en-port.

 

通过比较设备配置与可用的候选资源,Android从drawable-en-port中选择可绘画对象。

 

The system arrives at its decision for which resources to use with the following logic:

 

系统用以下逻辑达成它使用哪个资源的决定。

 

-------------------------------

 

(图2略:

1. 消除任意与设备配置冲突的目录。

2. 标识在表中的下一个修饰符——首先是MCC,然后是NNC,然后是语言,如此类推。

3. 有任意项匹配此修饰符吗?

否,转至2

是,转至4

4. 消除不包含此修饰符的目录(*)

转至2

5. 继续直至只剩下一个目录……。

 

* 如果修饰符是屏幕密度,Android选择一个“最佳”匹配并且完成该轮处理。

 

-------------------------------

 

Figure 2. Flowchart of how Android finds the best-matching resource.

 

图2. Android如何找到最佳匹配资源的流程图。

 

1. Eliminate resource files that contradict the device configuration.

 

1. 排除与设备配置冲突的资源文件。

 

The drawable-fr-rCA/ directory is eliminated, because it contradicts the en-GB locale.

 

drawable-fr-rCA/被排除,因为它与en-GB区域冲突。

 

drawable/

drawable-en/

drawable-fr-rCA/ (注:原文被划去,表示被消除或被无视)

drawable-en-port/

drawable-en-notouch-12key/

drawable-port-ldpi/

drawable-port-notouch-12key/

 

-------------------------------

 

Exception: Screen pixel density is the one qualifier that is not eliminated due to a contradiction. Even though the screen density of the device is hdpi, drawable-port-ldpi/ is not eliminated because every screen density is considered to be a match at this point. More information is available in the Supporting Multiple Screens document.

 

例外:屏幕像素密度是一个修饰符,它不会因为冲突而被忽略。即使设备的屏幕密度是hdpi,drawable-port-ldpi/不被忽略,因为此时每个屏幕密度被认为是匹配的。更多可用信息在多屏幕文档中支持。

 

-------------------------------

 

2. Pick the (next) highest-precedence qualifier in the list (table 2). (Start with MCC, then move down.)

 

2. 选取列表(表2)中(下一个)最高优先级修饰符。(从MCC开始,然后向下移动。)

 

3. Do any of the resource directories include this qualifier?

 

3. 是否有一个资源目录包含此修饰符?

 

* If No, return to step 2 and look at the next qualifier. (In the example, the answer is "no" until the language qualifier is reached.)

 

* 如果否,返回到步骤2并看下一个修饰符。(在该示例中,答案是“否”直至见到语言修饰符。)

 

* If Yes, continue to step 4.

 

* 如果是,继续执行步骤4。

 

4. Eliminate resource directories that do not include this qualifier. In the example, the system eliminates all the directories that do not include a language qualifier:

 

4. 忽略不包含此修饰符的资源目录。在该示例中,系统忽略所有不包含语言修饰符的目录:

 

-------------------------------

 

drawable/ (注:原文被划去)

drawable-en/

drawable-en-port/

drawable-en-notouch-12key/

drawable-port-ldpi/(注:原文被划去)

drawable-port-notouch-12key/(注:原文被划去)

 

-------------------------------

 

-------------------------------

 

Exception: If the qualifier in question is screen pixel density, Android selects the option that most closely matches the device screen density. In general, Android prefers scaling down a larger original image to scaling up a smaller original image. See Supporting Multiple Screens.

 

警告:如果讨论中的修饰符是屏幕像素密度,Android选择最接近设备密度的选项。通常,Android倾向于缩小一个较大的原始图片而非放大一个较小的原始图片。参见支持多屏幕。

 

-------------------------------

 

5. Go back and repeat steps 2, 3, and 4 until only one directory remains. In the example, screen orientation is the next qualifier for which there are any matches. So, resources that do not specify a screen orientation are eliminated:

 

5. 返回并重复步骤2,3,和4直至剩余仅一个目录。在示例中,屏幕方向是任意匹配的下一个修饰符。这样,不是指定屏幕方向的资源被忽略:

 

drawable-en/(注:原文被划去)

drawable-en-port/

drawable-en-notouch-12key/(注:原文被划去)

 

The remaining directory is drawable-en-port.

 

剩下的目录是drawable-en-port。

 

Though this procedure is executed for each resource requested, the system further optimizes some aspects. One such optimization is that once the device configuration is known, it might eliminate alternative resources that can never match. For example, if the configuration language is English ("en"), then any resource directory that has a language qualifier set to something other than English is never included in the pool of resources checked (though a resource directory without the language qualifier is still included).

 

虽然这个优先级对于每个请求的资源都执行。但是系统进一步优化一些切面。其中一个优化是,一旦设备配置是已知的,它可能忽略不可能匹配的可选资源。例如,如果配置语言是英语("en"),那么任意拥有一个设置为不是英语的语言修饰符的资源目录从不包含在资源检查的池中(尽管一个不带语言修饰符的资源目录仍然被包含)。

 

When selecting resources based on the screen size qualifiers, the system will use resources designed for a screen smaller than the current screen if there are no resources that better match (for example, a large-size screen will use normal-size screen resources if necessary). However, if the only available resources are larger than the current screen, the system will not use them and your application will crash if no other resources match the device configuration (for example, if all layout resources are tagged with the xlarge qualifier, but the device is a normal-size screen).

 

当基于屏幕大小修饰符选择资源时,系统将使用设计的屏幕比当前屏幕小的资源,如果没有更好匹配的资源(例如,一个大尺寸屏幕将使用普通尺寸屏幕资源,如果需要的话)。然而,如果唯一可用的资源比当前屏幕大,那么系统将使用它们,而你的应用程序将崩溃,如果没有其它资源匹配设备配置(例如,如果所有布局资源用xlarge修饰符标注,但设备是一个普通尺寸的屏幕)。

 

Note: The precedence of the qualifier (in table 2) is more important than the number of qualifiers that exactly match the device. For example, in step 4 above, the last choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen type, and input method), while drawable-en has only one parameter that matches (language). However, language has a higher precedence than these other qualifiers, so drawable-port-notouch-12key is out.

 

注意:修饰符的优先级(在表2中)比精确匹配设备的修饰符数量更重要。例如,在上面步骤4中,列表上最后的选项包含三个精确匹配设备的修饰符(方向,触摸屏类型,以及输入方法),然而drawable-en只有一个匹配的参数(语言)。然而,语言拥有比其它这些修饰符更高的优先级,所以drawable-port-notouch-12key被排除。

 

To learn more about how to use resources in your application, continue to Accessing Resources.

 

想知道更多关于如何在你的应用程序中使用资源,请继续看访问资源章节。

 

-------------------------------

 

Known Issues

 

已知问题

 

Android 1.5 and 1.6: Version qualifier performs exact match, instead of best match

 

Android 1.5和1.6:版本修饰符执行精确匹配,而非最佳匹配

 

The correct behavior is for the system to match resources marked with a version qualifier equal to or less than the platform version on the device, but on Android 1.5 and 1.6, (API level 3 and 4), there is a bug that causes the system to match resources marked with the version qualifier only when it exactly matches the version on the device.

 

正确行为是让系统能匹配标注有一个版本修饰符等于或小于设备上平台版本的资源,但在Android 1.5和1.6上(API级别3和4),有一个缺陷导致系统用版本修饰符匹配资源仅当它准确地匹配设备上的版本。

 

The workaround: To provide version-specific resources, abide by this behavior. However, because this bug is fixed in versions of Android available after 1.6, if you need to differentiate resources between Android 1.5, 1.6, and later versions, then you only need to apply the version qualifier to the 1.6 resources and one to match all later versions. Thus, this is effectively a non-issue.

 

权宜之计:为了提供版本特定的资源,遵照此行为。然而,因为这个缺陷在1.6以后可用的Android版本中修复,如果你需要区分Android 1.5,1.6之间以及更新版本的资源,那么你只需要应用版本修饰符到1.6资源,和一个匹配所有较新版本(注:的资源)。这样,它实际上是个不大的问题。

 

For example, if you want drawable resources that are different on each Android 1.5, 1.6, and 2.0.1 (and later), create three drawable directories: drawable/ (for 1.5 and lower), drawable-v4 (for 1.6), and drawable-v6 (for 2.0.1 and later—version 2.0, v5, is no longer available).

 

例如,如果你希望可绘画资源在每个Android 1.5、1.6和2.0.1(以及更新)版本上不同,请创建三个可绘画对象目录:drawable/ (用于1.5和更低),drawable-v4(用于1.6),以及drawable-v6(用于2.0.1和更新——版本2.0,即v5,不再可用)。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 21 Dec 2011 3:15

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值