屏幕高度为 640 dp 的 xxhdpi 设备使用 xhdpi 而不是 h640dp-xhdpi

xxhdpi devices with the screen height of 640 dp use xhdpi rather than h640dp-xhdpi

我在以下文件夹中有资源:

值-xhdpi

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="test_str">values-xhdpi</string>

    <style name="test">
        <item name="android:background">#aaaaff</item>
    </style>
</resources>

值-mdpi

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="test_str">values-mdpi</string>

    <style name="test">
        <item name="android:background">#ffaaaa</item>
    </style>
</resources>

values-sw600dp

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="test_str">values-sw600dp</string>

    <style name="test">
        <item name="android:background">#aaffaa</item>
    </style>
</resources>

值-h640dp-xhdpi

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="test_str">values-h640dp-xhdpi</string>

    <style name="test">
        <item name="android:background">#cacaca</item>
    </style>
</resources>

我 运行 在 Nexus 5 上使用这些资源的测试应用程序。屏幕密度为 xxhdpi,最小宽度为 360 dp。 (1920 * 1080 像素 = 640 * 360 dp)。

Android如何找到最匹配资源的流程图:


我的配置限定符名称应按以下顺序检查(请参阅 Table 2. 配置限定符名称 here):


让我们检查一下 smallestWidth 并消除与设备配置相矛盾的资源:

值-xhdpi

值-mdpi

values-sw600dp

值-h640dp-xhdpi.


让我们检查一下可用高度并消除与设备配置相矛盾的资源:

值-xhdpi

值-mdpi

值-h640dp-xhdpi.

所以现在 values-h640dp-xhdpi 是唯一剩下的资源文件夹。让我向您展示我是如何应用资源的:

<TextView
    style="@style/test"
    android:text="@string/test_str"
    android:textSize="46sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

但是,当我 运行 我的应用程序在 Nexus 5 上时,它使用来自 values-xhdpi 的资源。为什么?


更新#1

这是我错的地方:

Let's examine Available height and eliminate the resources that contradict the device configuration:

values-xhdpi

values-mdpi

values-h640dp-xhdpi.

values-h640dp-xhdpi因矛盾被淘汰(见解释原因),values-mdpivalues-xhdpi 被留下。 Nexus 5 的密度是 xxhdpi,但是没有为 xxhdpi 指定资源。所以最好的匹配是小屏幕的资源,xhdpi。

Supporting Multiple Screens

Specifically, when selecting resources based on the 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

Nexus 5 是:- 445 ppi 像素密度

三星 Galaxy Nexus I9250:- 316 ppi 像素密度

这就是它使用来自 value-xhdpi 的资源的原因。

values-h640dp-xhdpi - 三星 Galaxy Nexus I9250

xhdpi 值 - Nexus 5

在文档中我们可以看到

When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen height.

Nexus 5 屏幕高度是 567,但你的高度是 640,这就是为什么 Android 排除这个文件夹。