开发规范一点的项目或者企业级项目,大家都知道,页面中出现的字和图片等资源不能直接出现在代码中,只能在代码中引用,而这些资源文件必须放在固定的目录下统一管理,以便让系统根据情况使用不同的资源文件。最常见的一种情况,就是国际化,也就是Web开发中经常提到的internationalization(简称i18n),开发的应用如果给中国人用,那语言就是中文,程序中直接引用中文的资源文件就可以了,同样,如果给美国人用,只需让系统引用英文的资源文件即可。而程序中是很容易根据情况更改对资源的引用的,甚至不需要修改代码。
Android应用中的资源文件也一样,大屏、高分辨率的手机就使用大图片,小屏手机就使用小分辨率图片,中文系统下就用中文的字符资源文件,英文系统下就用英文的字符资源文件。那我们应该怎样来管理这些资源文件呢?
应该把像图片、字符串等资源文件从Java代码中分离出来,以便于能够独立地去维护它们,这样就可以为不同语言不同配置不同分辨率的手机指定合适的资源文件了。首先需要有系统默认使用的资源文件,当系统找不到适合当前系统配置的资源文件时就使用这些默认的资源文件。其次需要一些适合特定系统配置的资源文件,并给它们的目录名添加相应的限定词。例如对于浏览新闻的页面,对于窄屏的手机来说,整个界面就显示一个标题列表,点击标题跳转到其它页面查看新闻详情,此时可以把一个ListView的布局文件放到res/layout/目录下;而对于宽屏的平板来说,屏幕很大,可以左边显示新闻列表右边显示新闻详情,此时可以把这个复杂一点的布局文件放到res/layout-large目录下。这样Android系统就可以自动的根据当前屏幕的宽度选择要使用的布局文件了。
所有的资源文件都放在res/目录下,然后资源文件再按照类型进行分组:
注意,永远不要把资源文件直接放到res/目录下,那样会导致编译错误。
限定符的格式是<resources_name>-<config_qualifier>
resources_name就是默认的资源目录名,如drawable、values
config_qualifier就是限定符,限定这个目录下的资源文件是给哪个特定配置使用的。多个限定符之间用破折号(-)隔开
如:
res/ drawable/ icon.png background.png drawable-hdpi/ icon.png background.png
限定符hdpi表明这个目录下的资源文件是给高分辨率设备用的,高分辨率设备会优先到该目录下寻找资源文件,如果找不到,再使用默认的drawable目录下的资源文件。两个目录下的资源文件名字必须完全一致,因此资源ID也是一致的。
一个目录可以添加多个限定符,用破折号(-)隔开,但必须按下面的优先级顺序定义:
官网的表:
Configuration | Qualifier Values | Description |
---|---|---|
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 onlyfor 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 |
Round screen | round notround |
Added in API level 23. 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, watch added in API 20. 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 xxhdpi xxxhdpi nodpi tvdpi |
There is a 3:4:6:8:12:16 scaling ratio between the six primary densities (ignoring the tvdpi density). So, a 9x9 bitmap in ldpi is 12x12 in mdpi, 18x18 in hdpi, 24x24 in xhdpi and so on. 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 areonly 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, |
Note