when we provide a number of different resource files available for Android when the system is selected, Android The resource that best matches the current configuration is selected at run time according to a set of rules that are adapted. To illustrate how Android chooses resources, let's say we have the following optional resource file directories, each containing a different version of the same picture.
drawable/drawable-en/drawable-fr-rca/drawable-en-port/drawable-en-notouch-12key/drawable-port-ldpi/ drawable-port-notouch-12key/
Assume that the system configuration is as follows:
Locale = en-GB
Screen orientation = port
Screen pixel density = hdpi
Touchscreen type = notouch
Primary Text Input method =12key
After comparing the system configuration with the optional resources,Android chooses drawable-en-port/
Android chooses resources according to the following logic
1. Exclude resource files that are different from the system configuration
drawable-fr-rca/ was ruled out because it violated the LOCALE=EN-GB .
drawable/drawable-en/drawable-fr-rca/drawable-en-port/drawable-en-notouch-12key/drawable-port-ldpi/ drawable-port-notouch-12key/
exception: screen pixel density (pixel density) This qualifier is not simply excluded because it differs from the current system configuration. Even if the current screen configuration is hdpi,drawable-port-ldpi/ will not be excluded because each screen density is considered adaptable. Read more about supporting multiple Screens.
2.Select the highest-priority qualifier (the next) in the list (start at MCC, then lower down).
3.is there a resource directory containing this qualifier?
No, return to step 2 and look for the next qualifier (in the example, whether the answer is until the language qualifier is found).
Yes, proceed to step 4 .
4. Exclude the directory of resource files that do not contain this qualifier. In this example, the system excludes all directories that do not contain language qualifiers:
drawable/drawable-en/drawable-en-port/drawable-en-notouch-12key/drawable-port-ldpi/ drawable-port-notouch-12key/
Exception: If the qualifier is screen pixel density (device screens density), Android chooses one that is closest to the pixel density of the screen. In short, Android is more inclined to scale the original large picture into smaller images. Refer to supporting multiple Screens.
5. Return to repeat steps 2,3, and 4 to know that there is only one directory left. In this example, the screen orientation is the next qualifier used to match the resource. Therefore, the resource file directory without a restricted screen orientation is excluded.
drawable-en/drawable-en-port/drawable-en-notouch-12key/
The last remaining directory is Drawable-en-port .
While each resource file is requested, the process is performed as described above, but the system itself performs the optimization of the lookup process. One of the optimizations is that once the configuration of the system has been loaded, it may eliminate the optional resources that will never be matched. For example, if the language in the configuration is English ("en"), then any other language qualifier that is not English will be excluded from the optional Resource directory pool (but if there is no language qualifier in the resource directory, Then it is still optional).
When you select resources based on the screen size, if you cannot find a resource that matches the current screen, the resources that are designed for a smaller screen than the current one will be used (for example, a large-size screen will be used when necessary The resource file for the normal-size screen). However, if the only alternative resource is larger than the current screen, the system will not use them and your program will crash if no other resources can match the system configuration (for example, all layout resources have XLarge , but the current device screen is normal-size ).
Note:On the exact matching resource, the precedence of the qualifier (in the table2) is more important than the number of qualifier words. For example, in the steps above4, the last item in the list also contains three resources that accurately match the system configuration (Orientation,Touchscreen typeand theInput Method), howeverDrawable-enthere's only one match .(language). However, the language has a higher priority than the other qualifiers, soDrawable-port-notouch-12keywas ruled out.
This article is translated from How Android Finds the best-matching Resource
How to find the optimal adaptation resources for Android