سؤال

If I have only one style file in values folder and it contains items refer to items from dimens file then I don't get right result. Only if I will have style file in each values folder (MDPI, HDPI, etc) I will get right result.

That is a bit strange because for drawable I could have files only in one folder and resources for different DPI in other folders and it works fine. Could someone explain how Android search style items when I use references from dimens?

Here is a little example.

values/styles.xml

<resources>
    <style name="message_item_topic">
        <item name="android:textSize">@dimen/message_id_topic</item>
    </style>
</resources>

values/dimens.xml

<resources>
    <dimen name="message_id_topic">12sp</dimen>
</resources>

values-hdpi/dimens.xml

<resources>
    <dimen name="message_id_topic">15sp</dimen>
</resources>
هل كانت مفيدة؟

المحلول 2

If you were using px, cm, in, ... then a different dimens.xml in separate values-(l|m|h|xh|xxh)dpi would make sense.

Things are relative.
And there are too many devices around.
You'll never be sure your app will fit on EVERY existing device.
Some users will contact you and ask a fix for their device.
So, you'll read the specs, make an emulator, add the specific drawable/values and rerelease your app. Keep in mind that TABLETS will require special drawable/values folders.

نصائح أخرى

As per the Providing Alternate Resources guide:

Android supports several configuration qualifiers and you can add multiple qualifiers to one directory name, by separating each qualifier with a dash. Table 2 lists the valid configuration qualifiers, in order of precedence—if you use multiple qualifiers for a resource directory, you must add them to the directory name in the order they are listed in the table.

The rules for finding the best matching resource:

  1. Eliminate resource files that contradict the device configuration. (Exception: DPI)
  2. Pick the (next) highest-precedence qualifier in the list
  3. Do any of the resource directories include this qualifier?
    • If No, return to step 2 and look at the next qualifier. (In the example, the answer is "no" until the language qualifier is reached.)
    • If Yes, continue to step 4.
  4. Eliminate resource directories that do not include this qualifier

As per the flowchart:

flowchart

Each resource is loaded according to these rules separately (i.e., a lookup for each dimen happens for each, independent of the other resources). Any display issues you are having are probably due to not knowing the difference between things that scale by DPI (dp and sp) and things that do not (px) - use dp and sp and you do not need to declare alternate resources (for anything but drawables) for different DPI devices.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top