可使用的资源文件(Providing Resources)
你应该将代码和资源文件(比如图片和字符串)分开,这样你可以单独的来维护这些资源文件。你也可以通过特殊的名字的文件路径,为特定的设备配置提供可替换的资源文件。Android会基于现在的配置自动应用合适的资源文件。例如,你可以根据不同的屏幕尺寸提供不同的UI布局。
一旦你将资源文件与程序代码分开保存,你可以通过资源IDs来使用对应的资源文件。这些资源ID在项目的R类生成。关于如何在你的程序里面使用资源,可以看看 Accessing Resources
Grouping Resource Types/资源文件的分类
在工程的res/目录下,每个子目录对应一种类型的资源文件。例如,下面是一个简单工程的文件树状结构。
MyProject/
src/
MyActivity.java
res/
drawable/
icon.png
layout/
main.xml
info.xml
values/
strings.xml
res/目录下包含了所有的资源文件(在子目录下):图片资源类型,两种布局资源文件,和一个string资源文件。这个资源目录的名字是非常重要的,如Table1
Table1. 工程res/下面可以支持的资源目录:
目录 | 资源类型 |
anim/ | 定义动画tween的XML文件,见 Animation Resources |
color/ | 定义了一些colors对应值的XML文件,见Color State List Resource |
drawable/ |
Bitmap文件(.png, .9.png, .jpg, .gif)或者能被编译为drawable类型的XML文件
见 Drawable Resources |
menu/ | 定义程序menus的XML文件,比如Options Menu, Context Menu, 或者Sub Menu.见Menu Resource |
raw/ |
放置任意的文件,必须为原始形式(Arbitrary files to save in their)。这个文件夹的文件不能是被压缩过的(Files in here are not compressed)。使用raw InputStream来打开这些资源文件,调用Resources.openRawResource()方法来打开resource ID对应的资源,比如R.raw.filename。 然而,如果你需要访问原始文件名字和文件目录结构,你可以考虑将一些资源文件保存在 assets/ 目录下(代替res/raw/)。放在assets/下的文件不需要给一个资源ID,所以你可以使用 AssetManager来读取它们。 |
values/ |
包含一些简单值的XML文件,比如strings,integers,和颜色 在其他res/子目录下定义的XML资源文件定义的是一个单独的基于XML文件名的资源(Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename),在values/目录下的文件描述了多种资源。例如这个目录下的某个文件,每个<resources>的子节点定义了一个单独资源。比如,<string>节点创建了一个R.string资源,<color>节点创建了一个R.color资源。 因为每个资源使用自己的XML文件定义的,你可以任意命名文件,并在一个文件里面放不同种类的资源文件。但是,为了清晰可见(for clarity),你也许想要在一个文件里面放置统一的资源类型。例如,下面是这个目录下创建资源文件的俗称约定:
可以看看 String Resource,Style Resource 以及 More Resource Types。 |
xml/ | 该文件下的xml可以在运行时调用Resources.getXML()来读取。各种XML配置文件都必须保存在这里,比如searchable configuration |
注意:你不可以直接将资源文件保存在res/目录下。
Resource Types 文档有更多资源类型介绍。
Accessing Resource文件介绍了如何用代码访问 res/子目录下的资源文件。
Providing Alternative Resources/提供可替换的资源
几乎每个程序都应该给特定的设备配置提供可替换的资源。例如,可以为不同的屏幕尺寸提供可替换的资源,不同的语言提供可替换的字符串。运行时,Android会自动检查当前屏幕配置并加载合适的资源。
要指定一组资源配置的具体方案(To specify configuration-specific alternatives for a set of resources):
- 在res/下创建一个新的目录 <resources_name>-<config_qualifier>
- <resources_name>是和默认资源相对应的目录名称
- <config_qualifier>是指定一个配置相应的名称,同时放在这个目录下的资源会在这个配置上使用。你可以附加多个<config_qualifier>。用破折号进行分开。
- 在你新的目录下保存你的可替换资源,这个资源文件必须和默认的资源命名一样。
例如,下面是一些默认资源和可替换资源:
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
这个hdpi意思是说在这个目录下的资源将会在高分辨率(hign-density)的设备上使用。同时每个drawable目录下的图片根据特定屏幕密度绘制大小,文件名是相同的。这样,你使用引用icon.png和backgrou.png的资源ID是相同的,但是android会根据当前设备配置选择最合适的drawable。
Android支持多种配置标识符(qualifiers),你可以给你一个目录名字增加多个标识符(qualifiers),通过破折号分开。Table 2列出了合法的配置标识符(qualifiers),如果你使用了多个标识符,则按顺序来决定优先权,资源必须防止在下表里出来的并以此命名的文件夹下面:
Table 2 . 可替换资源标识符名字
Qualifier | Values | Description |
MCC and MNC | Examples: mcc310 mcc310-mnc004 mcc208-mnc00 etc. |
MCC和MNC分别是从设备的SIM卡读取出来的mobile country code(MCC),其实是可选的mobile network code(MNC)。 例如,mcc310是U.S on any carrier, mcc310-mnc004 is U.S on Verizon, and mcc208-mnc00 is France on Orange. region 标识符代替(下面会讨论)。如果你决定使用MCC和MNC标识符,你应该仔细测试它是否可以正常运行。 |
Language and region | Examples: en fr en-rUS fr-rFR fr-rCA etc. |
语言被定义为ISO 639-1 代码的两个字母,可以选择 ISO 3166-1-alpha-2区域(小写的“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 applicati on during runtime.) |
Screen size | small normal large |
small: 基于低密度QVGA屏幕可用空间的屏幕(Screens based on the space available on a low-density QVGA screen) 。Considering a portrait HVGA display, this has the same available width but less height—it is 3:4 vs. HVGA’s 2:3 aspect ratio. Examples are QVGA low density and VGA high density. at least this size(independent of density) and not larger. Examples of such screens a WQVGA low density, HVGA medium density, WVGA high density. more available space in both width and height than an HVGA display. Examples are VGA and WVGA medium density screens. |
Wider/taller screens | long notlong |
long:长屏幕,比如 WQVGA,WVGA,FWVGA notlong:不是长屏幕,比如QVGA,HVGA,和VGA 这个纯粹基于屏幕的长宽比(aspect ratio)(一个长屏幕是宽的)。且它与屏幕的方向是不相关的。 也可以看看screenLayout配置域,标明了屏幕是否是long的。 |
Screen orientation | port land |
port:设备处于纵向(垂直)。Device is in portrait orientation(vertical) rotates the screen.)。Handling Runtime Changes 介绍了是如何在运行时影响你的程序的。 |
Dock mode | car desk |
car:Device is in a car dock disable this mode using UiModeManager. See Handling Runtime Changes for information about how this affects your application during runtime. |
Night mode | night notnight |
night: Night time the mode changes based on the time of day. You can enable or disable this mode using UiModeManager. See Handling Runtime Changes for infor |
Screen pixel density(dpi) | ldpi mdpi hdpi nodpi |
ldpi:低分辨率的屏幕,大约 120dpi。Low-density screens; approximately 120dpi. do not want to be scaled to match the device density. |
Touchscreen type | notouch stylus finger |
notouch:设备没有触摸屏。Device does not have a touchscreen stylus:设备为电阻触屏,适合手写笔。Device has a resistive touchscreen that’s suited for use with a stylus. finger:设备为触摸屏。 可以看看 touchscreen,标明了设备的触摸屏类型。 |
Keyboard availability | keysexposed 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.) keyboard available but it is hidden and the device does not have a software keyboard enabled). 有一个可用的软件键盘(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)。 e Changes for information about how this affects your application during runtime. configuration fields hardKeyboardHidden and keyboardHidden, which indicate the visibility of a hardware keyboard and the visibility of any kind of keyboard(including software), respectively) |
Primary text input method | nokeys qwerty 12key |
nokeys:Device has no hardware keys for text input qwert:Device has a hardware qwerty keyboard, whether it’s visible to the user or not 12key. 12key:Device has a hardware 12-key keyboard, whether it’s visible to the user or not. |
Navigation key availability | navexposed navhidden |
navexposed: Navigation keys are available to the user Changes for information about how this affects your application during runtime. |
Primary non-touch navigation method | nonav dpad trackball wheel |
nonav:Device has no navigation facility other than using the touchscreen. dpad:Device has a directional-pad (d-pad) for navigation. trackball:Device has a trackball for navigation. wheel: Device has a directional wheel(s) for navigation(uncommon). Alson see the navigation configuration field, which indicates the type of navigation method available. |
API Level | Examples: v4 v5 v6 v7 etc |
The API Level supported by the device, for example v1 for API Level 1(Android 1.0) or v5 for API Level 5 (Android 2.0). See the Android API Levels document for more information about these values. |
标识符名称的规则
下面是使用资源标识符名称的一些规则:
- 你可以为一组类型的资源指定多个标识符,并用破折号进行分开。例如,drawable-en-rUS-land的资源将用于US-English并且此时为横屏的设备上。
- 标识符的顺序必须与table 2相同,例如:
- Wrong:drawable-hdpi-port/
- Correct:drawable-port-hdpi/