简体   繁体   中英

Android drawable handling based on qualifiers

A friend asked me to help debug her app. The app provides drawables for many types of device configurations.

On Samsung Galaxy Mini, the app crashes, because it can't find any matching resources. (If all resources are copied to drawable folder, it fixes the problem.) I'm wondering about the reason of the crash.

The content of the res folder is as follows:

Res文件夹

The only places where actual resources are stored are drawable-large-tvdpi and the drawable-normal-* folders. The rest of the folders contain only a test image for debug purposes (eg drawable-mdpi contains a picture with the text 'MDPI', so the app can use it to detect where the current device searches for resources). Obviously, the crash is not due to these debug images, but because the app can't find the actual resources referenced by the app's XML.

The Galaxy Mini has a "small" screen and an "ldpi" density. My guess is that since none of the folders that contain actual resources are for "small" screen size (they either have the "normal" or "large" qualifier), Android will not find any matching resource. (Remember that not all folders store the actual resources.) How should I reorganize everything to ensure that the app doesn't crash on any device at all?

(Note that I certainly read the relevant official Android doc .)

Okay, this gets a little confusing, but the issue is (kind of) articulated in the docs. Specifically:

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 first].

and

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 larger than 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).

So, the issue is that you are providing density specific resources for normal/large screen qualifiers (eg drawable-normal-ldpi ), but none for small ( drawable-small-ldpi ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM