Android app项目中,res是用来存放资源文件的,来看看这些文件的创建和选取规则:
系统启动一个apk后,生成UI的过程中,会根据不同的系统配置来匹配、选择相应的资源文件。
You should also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. At runtime, Android uses the appropriate resource based on the current configuration.
Almost every application should provide alternative resources to support specific device configurations.
每个APK都应该提供多个资源来支持指定的设备配置。
At runtime, Android detects the current device configuration and loads the appropriate resources for your application.
运行时,Android会检测目前的设备配置,并为你的应用加载合适的资源。
资源文件夹的名称形如: <resources_name>-<config_qualifier>
A、<resources_name> is the directory name of the corresponding default resources。 // 指示不同的资源类型
anim === XML files that define tween animations.
color === XML files that define a state list of colors.
drawable=== Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes: Bitmap files、Nine-Patches (re-sizable bitmaps)、State lists、Shapes、Animation drawables、Other drawables
layout === XML files that define a user interface layout.
menu === XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu.
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.
values === XML files that contain simple values, such as strings, integers, and colors. types: arrays.xml、colors.xml、values、dimens.xml、strings.xml、styles.xml
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.
B、<qualifier> is a name that specifies an individual configuration for which these resources are to be used。
Android supports several configuration qualifiers and you can add multiple qualifiers to one directory name, by separating each qualifier with a dash.
常用的限定词有:
1. MCC and MNC: 如 mcc310-mnc004、mcc208-mnc00
indicate the current mobile country code and mobile network code, respectively.
2. Language and region: 如 fr、en-rUS
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").
3. Layout Direction: 两个值 ldrtl、ldltr
The layout direction of your application. ldrtl means "layout-direction-right-to-left". ldltr means "layout-direction-left-to-right" and is the default implicit value.
主要用于提供一些针对不同语言输出方向的布局。例如: layout-ldrtl/main.xml (Any "right-to-left" language, such as "ar" language for Arabic)
4. smallestWidth: sw<N>dp,如: sw600dp、sw720dp
指可被程序用户界面使用的屏幕可用空间的最小值(单位为dp)指可用屏幕两边中最短的那条边长。为了保证与应用程序兼容,设备的smallestWidth必须大于等于本属性值。(通常此值对应于布局layout所支持的“最小宽度”,而与屏幕当前的方向无关。)
5. Available w<N>dp,如:w720dp、w1024dp
Specifies a minimum available screen width, in dp units at which the resource should be used—defined by the <N> value.
该值会根据屏幕的方向而发生改变,如若,应用提供了多个不同值,系统会取用最接近实际值的那一项。
6. Available height: h<N>dp,如:h720dp、h1024dp
Specifies a minimum available screen height, in "dp" units at which the resource should be used—defined by the <N> value.
与前一项类似。
7. Screen size: 枚举值,small、normal、large、xlarge
small: is approximately 320x426 dp units.
normal: is approximately 320x470 dp units.
large: is approximately 480x640 dp units.
xlarge: is approximately 720x960 dp units.
8. Screen aspect: 枚举值,long、notlong
long: Long screens, such as WQVGA, WVGA, FWVGA
notlong: Not long screens, such as QVGA, HVGA, and VGA
9. Screen orientation: 枚举值,port、land
port: Device is in portrait orientation (vertical)
land: Device is in landscape orientation (horizontal)
10. UI mode: 枚举值,car、desk、television、appliance
car: Device is displaying in a car dock
desk: Device is displaying in a desk dock
television: Device is displaying on a television, providing a "ten foot" experience where its UI is on a large screen that the user is far away from, primarily oriented around DPAD or other non-pointer interaction
appliance: Device is serving as an appliance, with no display
11. Night mode: 枚举值,night、notnight
night: Night time
notnight: Day time
12. Screen pixel density (dpi):枚举值,ldpi、mdpi、hdpi、xhdpi、nodpi、tvdpi
ldpi: Low-density screens; approximately 120dpi.
mdpi: Medium-density (on traditional HVGA) screens; approximately 160dpi.
hdpi: High-density screens; approximately 240dpi.
xhdpi: Extra high-density screens; approximately 320dpi. Added in API Level 8
nodpi: This can be used for bitmap resources that you do not want to be scaled to match the device density.
tvdpi: Screens somewhere between mdpi and hdpi; approximately 213dpi.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.
13. Touchscreen type: 枚举值,notouch、finger
notouch: Device does not have a touchscreen.
finger: Device has a touchscreen that is intended to be used through direction interaction of the user's finger.
14. Keyboard availability: 枚举值,keysexposed、keyshidden、keyssoft
15. Primary text input method: 枚举值,nokeys、qwerty、12key
16. Navigation key availability: 枚举值, navexposed、navhidden
17. Primary non-touch navigation method: 枚举值,nonav、dpad、trackball、wheel
18. Platform Version (API level): 如 v3、v4
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).
Android运行过程中,系统会根据当前的设备配置来选择合适的资源文件,基本原则如下:
1. 排除不合适的资源文件。
2. 按降序选取优先级最高的修饰词
3. 排除不含该修饰词的资源目录
4. 重复2、3,直到只包含一个目录