When you request a resource for which you provide alternatives, Android selects which alternative resource to use at runtime, depending on the current device configuration. To demonstrate how Android selects an alternative resource, assume the following drawable directories each contain different versions of the same images:

drawable/drawable-en/drawable-fr-rCA/drawable-en-port/drawable-en-notouch-12key/drawable-port-ldpi/drawable-port-notouch-12key/

And assume the following is the device configuration:

Locale =en-GB
Screen orientation =port
Screen pixel density =hdpi
Touchscreen type =notouch
Primary text input method =12key

By comparing the device configuration to the available alternative resources, Android selects drawables fromdrawable-en-port.

The system arrives at its decision for which resources to use with the following logic:

Figure 2.Flowchart of how Android finds the best-matching resource.

  1. Eliminate resource files that contradict the device configuration.

    Thedrawable-fr-rCA/directory is eliminated, because it contradicts theen-GBlocale.

    drawable/drawable-en/                      drawable-fr-rCA/          drawable-en-port/drawable-en-notouch-12key/drawable-port-ldpi/drawable-port-notouch-12key/

    Exception:Screen pixel density is the one qualifier that is not eliminated due to a contradiction. Even though the screen density of the device is hdpi,drawable-port-ldpi/is not eliminated because every screen density is considered to be a match at this point. More information is available in theSupporting Multiple Screensdocument.

  2. Pick the (next) highest-precedence qualifier in the list (table 2). (Start with MCC, then move down.)
  3. Do any of the resource directories include this qualifier?
    • If No, return to step 2 and look at the next qualifier. (In the example, the answer is "no" until the language qualifier is reached.)
    • If Yes, continue to step 4.
  4. Eliminate resource directories that do not include this qualifier. In the example, the system eliminates all the directories that do not include a language qualifier:
  5.                   drawable/        drawable-en/drawable-en-port/drawable-en-notouch-12key/                  drawable-port-ldpi/                          drawable-port-notouch-12key/        

    Exception:If the qualifier in question is screen pixel density, Android selects the option that most closely matches the device screen density. In general, Android prefers scaling down a larger original image to scaling up a smaller original image. SeeSupporting Multiple Screens.

  6. Go back and repeat steps 2, 3, and 4 until only one directory remains. In the example, screen orientation is the next qualifier for which there are any matches. So, resources that do not specify a screen orientation are eliminated:
                          drawable-en/          drawable-en-port/                      drawable-en-notouch-12key/          

    The remaining directory isdrawable-en-port.

Though this procedure is executed for each resource requested, the system further optimizes some aspects. One such optimization is that once the device configuration is known, it might eliminate alternative resources that can never match. For example, if the configuration language is English ("en"), then any resource directory that has a language qualifier set to something other than English is never included in the pool of resources checked (though a resource directorywithoutthe language qualifier is still included).

When selecting resources based on the screen size qualifiers, the system will use resources designed for a screen smaller than the current screen if there are no resources that better match (for example, a large-size screen will use normal-size screen resources if necessary). However, if the only available resources arelargerthan the current screen, the system willnotuse them and your application will crash if no other resources match the device configuration (for example, if all layout resources are tagged with thexlargequalifier, but the device is a normal-size screen).

Note:Theprecedenceof the qualifier (intable 2) is more important than the number of qualifiers that exactly match the device. For example, in step 4 above, the last choice on the list includes three qualifiers that exactly match the device (orientation, touchscreen type, and input method), whiledrawable-enhas only one parameter that matches (language). However, language has a higher precedence than these other qualifiers, sodrawable-port-notouch-12keyis out.

To learn more about how to use resources in your application, continue toAccessing Resources.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android开发——控件基础(五)RadioButton组
  2. android中的六大布局
  3. Android腾讯微薄客户端开发教程汇总
  4. android 关于google刘海屏的解决方案
  5. layout_gravity与android:gravity有什么
  6. android各个文件分析
  7. android:versionCode和android:versionNa
  8. Android跨进程通信IPC之13——Binder总结
  9. Android(安卓)SDK中tools详解
  10. android中的六大布局