Issue
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:
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-mdpivalues-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.
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
Solution
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.
Answered By - Bracadabra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.