简体   繁体   中英

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

I have resources in the following folders:

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

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

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

I run a test app with these resources on Nexus 5. The screen density is xxhdpi, and the smallest width is 360 dp. (1920 * 1080 px = 640 * 360 dp).

The flowchart of how Android finds the best-matching resource:

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


My configuration qualifier names should be examined in the following order (see Table 2. Configuration qualifier names here ):

  • smallestWidth;
  • Available height;
  • Screen pixel density (dpi).

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

values-xhdpi

values-mdpi

values-sw600dp

values-h640dp-xhdpi.


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

values-xhdpi

values-mdpi

values-h640dp-xhdpi.

So now values-h640dp-xhdpi is the only resource folder left. Let me show you how I apply the resources:

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

However, when I run my app on Nexus 5, it uses the resources from values-xhdpi . Why?

在此输入图像描述


Update #1

This is where I was wrong:

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

values-xhdpi

values-mdpi

values-h640dp-xhdpi.

values-h640dp-xhdpi is eliminated due to a contradiction (see the answer which explains why), and values-mdpi and values-xhdpi are left. The density of Nexus 5 is xxhdpi, but there is no resource specified for xxhdpi. So the best match would be the resource for a smaller screen, 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

In documentation we can see

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 screen height is 567, but your height is 640, thats why Android exclude this folder.

Nexus 5 is :- 445 ppi pixel density

Samsung Galaxy Nexus I9250 :- 316 ppi pixel density

That why It's Uses the Resources from value-xhdpi.

values-h640dp-xhdpi - Samsung Galaxy Nexus I9250

value-xhdpi - Nexus 5

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