重生百将图txt下载奇书:可使用的资源文件(Providing Resources)

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 13:39:03
可使用的资源文件(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文件
Bitmap files Nine-Patches(re-sizable bitmaps)State lists Color drawables Shapes Animation drawables
见 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/目录下的文件描述了多种资源。例如这个目录下的某个文件,每个的子节点定义了一个单独资源。比如,节点创建了一个R.string资源,节点创建了一个R.color资源。
因为每个资源使用自己的XML文件定义的,你可以任意命名文件,并在一个文件里面放不同种类的资源文件。但是,为了清晰可见(for clarity),你也许想要在一个文件里面放置统一的资源类型。例如,下面是这个目录下创建资源文件的俗称约定:
arrays.xml:放置资源数组(typed arrays) colors.xml:放置颜色值(color values) dimens.xml:放置长度值(dimension values) strings.xml:放置字符串值(string values) styles.xml:放置样式(styles)
可以看看 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/下创建一个新的目录 - 是和默认资源相对应的目录名称 是指定一个配置相应的名称,同时放在这个目录下的资源会在这个配置上使用。你可以附加多个。用破折号进行分开。
在你新的目录下保存你的可替换资源,这个资源文件必须和默认的资源命名一样。
例如,下面是一些默认资源和可替换资源:
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 . 可替换资源标识符名字
QualifierValuesDescription
MCC and MNCExamples:
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.
如果设备使用无线连接(radio connection)(GSM手机),MCC从SIM读取,同时MNC从设备所连接的网络中读取。
你也可以单独的使用MCC(例如,在应用程序中包含特定国家的法律资源)。如果你仅仅需要指定语言,那么使用 language and
region 标识符代替(下面会讨论)。如果你决定使用MCC和MNC标识符,你应该仔细测试它是否可以正常运行。
也可以看看配置域 mcc 和 mnc,分别指示当前移动国家代码和移动网络代码。
Language and regionExamples:
en
fr
en-rUS
fr-rFR
fr-rCA
etc.
语言被定义为ISO 639-1 代码的两个字母,可以选择 ISO 3166-1-alpha-2区域(小写的“r”开头)的两个字母。
该代码不区分大小写,r前缀用于区分该地区的某个地方,你不能单独的指定一个区域。
如果用户改变了他系统设置里面语言,在你程序运行时就改变。可以看看 Handling Runtime Changes里面关于如何在程序运
行时发生变化的文档。(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.)
Localization完整的介绍了如何根据其他语音本土化你的程序。
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.
normal:Screen based on the traditional medium-density HVGA screen. A screen is considered to be normal if it is
at least this size(independent of density) and not larger. Examples of such screens a WQVGA low density, HVGA
medium density, WVGA high density.
large: Screens based on the space available on a medium-density VGA screen. Such a screen has significantly
more available space in both width and height than an HVGA display. Examples are VGA and WVGA medium
density screens.
See Supporting Multiple Screens for more information.
Also see the screenLayout configuration field, which indicates whether the screen is small, normal, or large.
Wider/taller screenslong
notlonglong:长屏幕,比如 WQVGA,WVGA,FWVGA
notlong:不是长屏幕,比如QVGA,HVGA,和VGA
这个纯粹基于屏幕的长宽比(aspect ratio)(一个长屏幕是宽的)。且它与屏幕的方向是不相关的。
也可以看看screenLayout配置域,标明了屏幕是否是long的。
Screen orientationport
land
port:设备处于纵向(垂直)。Device is in portrait orientation(vertical)
land:设备处于横向(水平)。Device is in landscape orientation(horizontal)
如果用户在屏幕旋转,同时你的程序在运行,发生改变(This can change during the life of your application if the user
rotates the screen.)。Handling Runtime Changes 介绍了是如何在运行时影响你的程序的。
也可以看看 orientation 配置域,标明了当前设备的方向。
Dock modecar
desk
car:Device is in a car dock
desk:Device is in a desk dock
Added in API Level 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.
Night modenight
notnight
night: Night time
notnight: Day time
Add in API Level 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 infor
Screen pixel density(dpi)ldpi
mdpi
hdpi
nodpi
ldpi:低分辨率的屏幕,大约 120dpi。Low-density screens; approximately 120dpi.
mdpi:中等分辨率的屏幕,大约160dpi。Medium-density(on traditional HVGA) screens; approximately 160 dpi.
hdpi:高分辨率的屏幕,大约240dpi。High-density screens; approximately 240 dpi.
nodpi:这可以那些你不想为了匹配屏幕分辨率而进行拉伸的位图资源。This can be used for bitmap resources that you
do not want to be scaled to match the device density.
Touchscreen typenotouch
stylus
fingernotouch:设备没有触摸屏。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.)
keyshidden:设备有一个可用的硬件键盘,但是是被隐藏的。同时设备没有可用的软件键盘。(Device has a hardware
keyboard available but it is hidden and the device does not have a software keyboard enabled).
keyssoft:设备有一个可用的软件键盘,无论它是不是可用的状态。
如果你提供了keysexposed 资源,但是没有keyssoft资源,系统会使用keysexposed资源而忽视键盘是不是可见,只要系统
有一个可用的软件键盘(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)。
如果用户打开了一个硬件键盘的时候发生改变(This can change during the life of your application)。See Handling Runtim
e Changes for information about how this affects your application during runtime.
也可以看看hardKeyboardHidden和keyboardHidden,分别标明了一个硬件键盘的能见度和其他键盘的能见度。(Also see the
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 methodnokeys
qwerty
12keynokeys: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 availabilitynavexposed
navhidden
navexposed: Navigation keys are available to the user
navhidden: Navigation keys are not available(such as behind a closed lid)
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.
Primary non-touch navigation methodnonav
dpad
trackball
wheelnonav: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 LevelExamples:
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/
可替换资源目录不能嵌套。例如,你不能有 res/drawable/drawable-en/ Values对大小写不敏感,资源编译器会在处理前将目录名转换为小写,以避免不区分大小写的文件系统出现问题。Any capitalization in the names is only to benefit readability. (未完成)