资源分组类型:
animator/ |
XML files that define property animations. |
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.) |
color/ |
XML files that define a state list of colors. See Color State List Resource |
drawable/ |
Bitmap files (
See Drawable Resources. |
layout/ |
XML files that define a user interface layout. See Layout Resource. |
menu/ |
XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource. |
raw/ |
Arbitrary files to save in their raw form. To open these resources with a raw However, if you need access to original file names and file hierarchy, you might consider saving some resources in the |
values/ |
XML files that contain simple values, such as strings, integers, and colors. Whereas XML resource files in other 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:
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. |
注:不能在res目录下直接保存任何资源文件,否则会出现编译错误,不能通过编译
在以上的目录中,所有的资源为默认的资源信息,然而android设备多种多样,需要为不同的设备提供不同的资源以达到最佳的适配效果。
为不同设备配置最佳资源的规则:
1、 为不同的资源创建不同资源目录:res/<resources_name>-<config_qualifier>
.
resources_name:资源的种类,同上目录结构中信息
config_qualifier:设备配置信息
2、不同资源目录下,资源名称要相同
res/ drawable/ icon.png background.png drawable-hdpi/ icon.png background.png
3、 目录配置config_qualifier参考表:
- 多个限定符可以用于一个文件,限定符之间用“-”隔开:
drawable-en-rUS-land
- 限定符之间要严格按照顺序,顺序下表:
MCC and 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, If the device uses a radio connection (GSM phone), the MCC and MNC values come from the SIM card. 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. Also see the configuration fields |
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 " The codes are not case-sensitive; the 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 |
Layout Direction |
ldrtl ldltr
|
The layout direction of your application. This can apply to any resource such as layouts, drawables, or values. For example, if you want to provide some specific layout for the Arabic language and some generic layout for any other "right-to-left" language (like Persian or Hebrew) then you would have: res/ layout/ main.xml (Default layout) layout-ar/ main.xml (Specific layout for Arabic) layout-ldrtl/ main.xml (Any "right-to-left" language, except for Arabic, because the "ar" language qualifier has a higher precedence.) Note: To enable right-to-left layout features for your app, you must set Added in API level 17. |
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 has at least 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, 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). Some values you might use here for common screen sizes:
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. Added in API level 13. Also see the 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 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. Also see the 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 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. Also see the 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
|
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 Added in API level 4. See Supporting Multiple Screens for more information. Also see the |
Screen aspect |
long notlong
|
Added in API level 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 |
Screen orientation |
port 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 |
UI mode |
car desk television
|
Added in API level 8, television added in API 13. For information about how your app can respond when the device is inserted into or removed from a dock, read Determining and Monitoring the Docking State and Type. This can change during the life of your application if the user places the device in a dock. You can enable or disable some of these modes using |
Night mode |
night notnight
|
Added 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 |
Screen pixel density (dpi) |
ldpi mdpi hdpi xhdpi nodpi tvdpi
|
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. 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. 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. |
Touchscreen type |
notouch finger
|
Also see the |
Keyboard availability |
keysexposed keyshidden keyssoft
|
If you provide 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 |
Primary text input method |
nokeys qwerty 12key
|
Also see the |
Navigation key availability |
navexposed 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 |
Primary non-touch navigation method |
nonav dpad trackball wheel
|
Also see the |
Platform Version (API level) | Examples:v3 v4 v7 etc. |
The API level supported by the device. For example, |
- 限定符之间不能嵌套:
res/drawable/drawable-en/
iswrong - 不区分大小写,有资源编译器统一将目录名称编译成小写.
- 每一个文件只能有一个同类的限定修饰符:
drawable-rES-rFR/
. is wrong instead ofdrawable-rES/
anddrawable-rFR/
别名:
应用场景:有一个资源,你有不想当做默认的资源使用,需要在不同的资源目中使用,当然你可以将该资源copy到每一个对应的文件夹,然而这是一个非常麻烦的事情,一旦该资源的内容发生变化,那么你不得不去更改每一个目录下的信息。当然你也可以采用别名的方式去处理,你可以在默认的资源文件夹下面为该资源创建别名。
不是所有的资源都可以采用别名的方式去处理,animation, menu, raw, and other都可以采用别名,除了在xml/目录下的资源不能采用别名。
假设drawable-en和drawable-us都有一个drawable icon.png 那么你可以在drawable目录下放置drawable icon.png,分别在drawable-en和drawable-us下创建一个icon.xml 分别指向drawable下的icon.png
<?xml version="1.0" encoding="utf-8"?><bitmapxmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/icon"/>
layout:
<?xml version="1.0" encoding="utf-8"?><merge> <includelayout="@layout/main_ltr"/></merge>
values:
<?xml version="1.0" encoding="utf-8"?><resources> <stringname="hello">Hello</string> <stringname="hi">@string/hello</string></resources>
colors:
<?xml version="1.0" encoding="utf-8"?><resources> <colorname="yellow">#f00</color> <colorname="highlight">@color/red</color></resources>
最佳适配方式:
如果你的应用要支持不同设备,不同分辨率,那么提供默认的资源就非常重要。
假设你的应用支持多种语言,而且你将所有的语言描述都写在带有限定符的文件家中如:values-en,且没有提供默认的语言支持,那么一旦你的应用程序运行在中文的设备上,程序将会崩溃,最好的解决办法是提供默认的语言支持,即使用户不了解里面的内容,也比程序直接崩溃的好,上述就解决方案是:写一套语言描述的string.xml 放在没有修饰符的values中,当然string.xml中的内容你可以是中文的内容,也可以是法文。
同样的,如果你的应用支持横竖屏的话,你需要用横屏或者竖屏中的一个来做默认,假设横屏layout-land,竖屏layout-port,默认使用横屏的话,那么将横屏的文件夹更名为layout。
提供默认的资源非常重要不仅是因为有些设备的配置超出你的预料之中,而且android系统在新版本中的特性在某些老版本中是不支持。如果的你应用程序采用新版的sdk编写,而运行在老版本的设备上,一旦没有提供默认的支持,程序有可能会崩溃,因为程序需要新版的特性。假设你的应用程序中使用了“night mode(night/not night)”这个特性在API level 8 中添加,而该应用程序运行在API level 4的设备上,那么程序将不能访问诸如drawable-night之类文件夹下的资源,最好的办法是提供默认的支持drawable。
访问目录资源规则
android访问某个资源如何定位到某个文件夹是根据你运行程序设备的配置而言。假设有如下文件夹:
drawable/ drawable-en/ drawable-fr-rCA/ drawable-en-port/ drawable-en-notouch-12key/ drawable-port-ldpi/ drawable-port-notouch-12key/
同样假设当前设备的配置如下:
Locale =en-GB
Screen orientation =port
Screen pixel density =hdpi
Touchscreen type =notouch
Primary text input method =12key
那么应用程序如何找到最佳的适配目录drawable-en-port/,有一下几个规则:
- 剔除与设备配置相矛盾的文件夹,(screen size density 不参与排除)
- 选择最高优先匹配的限定符表2,由MCC逐步向下匹配
- 如果没有找到包含有限匹配的目录则执行上一步,有则执行下一步
- 排除不包含制定限制符的文件夹。
- 重复前面3个步骤,直到所剩余的文件夹为1个。
1、排除同设备配置矛盾的文件夹:
drawable/
drawable-en/
drawable-fr-rCA/
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
2、MCC,MNC木有,根据language排除: drawable/ drawable-en/ drawable-fr-rCA/ drawable-en-port/ drawable-en-notouch-12key/ drawable-port-ldpi/ drawable-port-notouch-12key/
3、根据屏幕方向排除: drawable/ drawable-en/ drawable-fr-rCA/ drawable-en-port/ drawable-en-notouch-12key/ drawable-port-ldpi/ drawable-port-notouch-12key/
相关推荐
《智能家居系统 Android资源类型与应用》 在当今的智能设备领域,Android系统因其开放性和灵活性,已成为智能家居系统的重要组成部分。本教程将深入探讨Android资源类型及其在智能家居系统中的应用,帮助学习者掌握...
移动互联网应用
智能家居系统应用
例如,使用 "@string/hello" 引用一个字符串资源,其中string是资源类型,hello是资源的名称。 总结来说,Android的资源文件管理是应用开发中一个非常重要和基础的部分。合理地使用和管理资源文件可以提高应用的...
- **平台特性**:Android平台支持多种设备类型,包括智能手机、平板电脑等,并提供了丰富的API供开发者使用。 #### 二、Android开发工具与资源 - **官方文档与教程**:Google为开发者提供了详细的官方文档和教程,...
对于Android系统源码,开发者可以查看AOSP(Android Open Source Project)来学习关于Android资源管理的实现细节。 【标签】"工具"可能指的是Android Studio提供的各种辅助工具,如Resource Manager、Layout ...
这篇内容将深入探讨Android资源文件名小写重命名的必要性、方法以及相关的开发实践。 一、Android资源文件名规范 1. 小写字母:Android Studio推荐使用全小写字母来命名资源文件,以避免因大小写不敏感的文件系统...
在Android中,Assets目录是一个可以存放任意类型文件的地方,不受资源ID的限制,适合用来存储非结构化的数据或文件。 该框架可能包含以下核心功能: 1. **资源下载**:框架会提供接口或服务,用于从远程服务器下载...
4. **资源类型** - **字符串资源**:在`values/strings.xml`中定义,支持格式化和本地化。 - **图片资源**:`drawable`目录下的PNG、JPEG、9-Patch等图像文件,`mipmap`目录用于启动图标。 - **布局资源**:`...
在Android应用开发中,图片资源扮演着至关重要的角色,它们为用户提供直观的视觉体验,增强交互性和美观性。本资源包“Android图片资源.zip”显然包含了一系列与Android平台相关的图像文件,非常适合Android开发者...
通过`Build` -> `Analyse APK`,我们可以查看APK的组成,包括各个资源文件的大小和类型。这有助于识别哪些资源在APK中并未被使用,从而确定是否需要删除。 2. Gradle插件 Android Studio的Gradle插件,如`Android ...
资源文件夹用来存放各种资源类型,如图片、字符串、样式等,而资源文件夹下的文件则通过特定的格式定义和组织这些资源。本文将详细介绍Android资源文件的分类、组织结构和使用方法。 首先,资源文件夹中存放的文件...
### Android的资源与国际化设置 #### 一、资源管理 **资源**是Android应用程序中所有非代码文件的...通过上述内容的学习,开发者可以更好地理解和掌握Android资源系统的使用方法,从而开发出更加高质量的应用程序。
Android开发资源整理 article:分类积累开发中一些不错的技术博文,主要针对某个知识点做讲解。 blog:包括国内外优秀开发者及其他们的博客。 project:整理开源社区中优秀的开源项目,话说看别人的代码也是一种很...
### Android的相关文件类型详解 #### 一、Java 文件——应用程序源文件 **Java** 是 Android 应用程序开发的主要编程语言。尽管近年来 Kotlin 的流行度有所上升,但 Java 仍然是 Android 开发的重要组成部分。...
因此,"Android资源文件批量分类处理"的主题应运而生,它旨在通过自动化工具和方法,提高开发者的工作效率。 Android资源文件通常被组织在特定的目录结构中,如`res/drawable`(图片资源)、`res/values`(字符串、...
在Android应用开发过程中,我们常常需要下载各种类型的网络资源,比如图片、音乐、视频或文档等。为了提供更好的用户体验,开发者通常希望能够在下载前预知这些资源的大小,从而合理安排下载计划,避免不必要的流量...
本示例项目“android 各种类型的头像实现”专注于利用SVG(Scalable Vector Graphics)技术来创建可自定义形状的头像视图。SVG是一种基于XML的矢量图像格式,它允许无限缩放而不会损失画质,非常适合在移动设备上...
在Android平台上,文件资源管理器是一个非常重要的工具,它允许用户查看、操作和管理设备上的文件和目录。这里我们关注的是"Android 文件资源管理器2.0"的源码,这是一个包含两个主要模块的实现:文件管理和任务管理...
Android系统提供了丰富的资源类型,如布局文件(layout)、字符串资源(strings)、图像资源(drawables)、颜色资源(colors)以及样式和主题(styles and themes)等。这些资源可以在不同的设备配置下动态加载,...