你应该将代码和资源文件(好比图片和字符串)分开,这样你能够单独的来维护这些资源文件。你也能够经过特殊的名字的文件路径,为特定的设备配置提供可替换的资源文件。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/目录下的文件描述了多种资源。例如这个目录下的某个文件,每一个<resources>的子节点定义了一个单独资源。好比,<string>节点建立了一个R.string资源,<color>节点建立了一个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/下建立一个新的目录 <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. 若是设备使用无线链接(radio connection)(GSM手机),MCC从SIM读取,同时MNC从设备所链接的网络中读取。 你也能够单独的使用MCC(例如,在应用程序中包含特定国家的法律资源)。若是你仅仅须要指定语言,那么使用 language and region 标识符代替(下面会讨论)。若是你决定使用MCC和MNC标识符,你应该仔细测试它是否能够正常运行。 也能够看看配置域 mcc 和 mnc,分别指示当前移动国家代码和移动网络代码。 Language and region Examples: 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 application 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 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) 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 mode car 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 mode night 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 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.) 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 Runtime 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 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 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 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/ 可替换资源目录不能嵌套。例如,你不能有 res/drawable/drawable-en/ Values对大小写不敏感,资源编译器会在处理前将目录名转换为小写,以免不区分大小写的文件系统出现问题。Any capitalization in the names is only to benefit readability. (未完成) 原创文章,转载请注明: 转载自Girl is coding 本文连接地址: Providing Resources/Android可以使用的资源文件