java - 基于限定符的 Android 可绘制对象处理

标签 java android android-resources android-drawable

一位 friend 让我帮助调试她的应用程序。该应用程序为多种类型的设备配置提供可绘制对象。

在 Samsung Galaxy Mini 上,应用程序崩溃,因为它找不到任何匹配的资源。 (如果所有资源都复制到drawable文件夹,问题就解决了。)我想知道崩溃的原因。

res文件夹内容如下:

Res folder

存储实际资源的唯一位置是drawable-large-tvdpidrawable-normal-* 文件夹。其余文件夹仅包含用于调试目的的测试图像(例如 drawable-mdpi 包含带有文本“MDPI”的图片,因此应用程序可以使用它来检测当前设备在哪里搜索资源).显然,崩溃不是因为这些调试图像,而是因为应用无法找到应用的 XML 引用的实际资源。

Galaxy Mini 具有“小”屏幕和“ldpi”密度。我的猜测是,由于没有包含实际资源的文件夹适用于“小”屏幕尺寸(它们具有“正常”或“大”限定符),Android 将找不到任何匹配的资源。 (请记住,并非所有文件夹都存储实际资源。)我应该如何重新组织一切以确保应用程序不会在任何设备上崩溃?

(注意我当然看过相关官方Android doc。)

最佳答案

好吧,这有点令人困惑,但这个问题在文档中有(某种程度上)阐明。具体来说:

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

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

因此,问题是您正在为普通/大屏幕限定符(例如 drawable-normal-ldpi)提供密度特定资源,但没有为小屏幕(drawable-small-ldpi )。

关于java - 基于限定符的 Android 可绘制对象处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17342981/

相关文章:

android - 如何在 Kotlin 中从 Android "strings.xml"访问值

Android 可绘制资源在调试文件夹中创建?

android - 为什么 R.color.white 不能在 Color.WHITE 可以工作的地方工作?

java - IntelliJ 从爆炸 war 中排除文件夹

java - 备用构造函数调用

java - Android studio 从 2.1 更新到 2.3 后出现错误(无法启动)

java - 具有 Spring Security 的 Spring MVC Controller 继承

android - 使用 gradle 编译 Twitter4j

android - 如何在类中不扩展 Activity 的情况下检查 Android 设备是 HDPI 还是 MDPI?

android - 是否有适用于 Android 的枚举字符串资源查找模式?