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

时间:2015-08-17 10:39:13

标签: android android-resources dpi

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

值-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>

值-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 px = 640 * 360 dp)。

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

The flowchart of how Android finds the best-matching resource

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

  • 最小宽度;
  • 可用高度;
  • 屏幕像素密度(dpi)。

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

值-xhdpi

值-MDPI

值-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中的资源。为什么呢?

enter image description here

更新#1

这是我错的地方:

  

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

     

值-xhdpi

     

值-MDPI

     

值-h640dp-xhdpi。

values-h640dp-xhdpi 由于矛盾而被淘汰(请参阅the answer解释原因), values-mdpi 值 - xhdpi 离开了。 Nexus 5的密度为xxhdpi,但没有为xxhdpi指定资源。因此,最佳匹配将是较小屏幕的资源,xhdpi。

Supporting Multiple Screens

  

具体而言,在根据大小限定符选择资源时,   系统将使用专为小于的屏幕设计的资源   当前屏幕,如果没有更好的匹配资源(for   例如,如果是大尺寸屏幕将使用正常大小的屏幕资源   必要)。但是,如果唯一可用的资源大于   在当前屏幕上,系统不会使用它们和您的应用程序   如果没有其他资源与设备配置匹配,则会崩溃

2 个答案:

答案 0 :(得分:3)

在文档中我们可以看到

  

当您的应用程序提供多个资源目录时   对于此配置的不同值,系统使用该值   最接近(不超过)设备的当前屏幕高度。

Nexus 5的屏幕高度为567,但你的身高是640,这就是Android排除此文件夹的原因。

答案 1 :(得分:-1)

Nexus 5是: - 445 ppi像素密度

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

这就是为什么它使用来自value-xhdpi的资源。

values-h640dp-xhdpi - 三星Galaxy Nexus I9250

value-xhdpi - Nexus 5