Android如何匹配最佳资源文件 layout / drawable, 比如 res/drawable-mdpi, res/layout-mdpi-976x600 等
匹配流程图:
下面是英文原文,
原网址是:http://developer.android.com/guide/topics/resources/providing-resources.html#BestMatch
代码分析以后有时间补充。
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:
The drawable-fr-rCA/
directory is eliminated, because it contradicts the en-GB
locale.
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 the Supporting Multiple Screensdocument.
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. See Supporting Multiple Screens.
drawable-en/drawable-en-port/drawable-en-notouch-12key/
The remaining directory is drawable-en-port
.
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 are largerthan the current screen, the system will not use them and your application will crash if no other resources match the device configuration (for example, if all layout resources are tagged with the xlarge
qualifier, but the device is a normal-size screen).
Note: The precedence of the qualifier (in table 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), while drawable-en
has only one parameter that matches (language). However, language has a higher precedence than these other qualifiers, so drawable-port-notouch-12key
is out.
To learn more about how to use resources in your application, continue to Accessing Resources.