How to use resource qualifier on layouts to distin

2019-04-14 20:29发布

问题:

I make an application with several layouts to target several devices like smartphones and tablets.

For the moment, I have the following folder to distinguish theses devices :

  • res/layout # for normal devices like Samsung Galaxy Nexus, S III, ...
  • res/layout-large # for tablet 7 inch on Android < 3.2
  • res/layout-sw600dp # for tablet 7 inch
  • res/layout-xlarge # for tablet 10 inch on Android < 3.2
  • res/layout-sw720dp # for tablet 10 inch

The solution is good but I have a problem with old smartphones like HTC Desire that are still in Android 2.2 or 2.3.

Their screen size category is normal and density is hdpi so there is any difference with Samsung Galaxy Nexus for example in qualifiers in resources.

As version of Android is 2.2 or 2.3, I can't use new resources qualifiers for screen size. So, I don't know how I can target specifically these devices with qualifiers to have different layouts between Smartphones with screens that are 4 inch and more and with others that are screens with less than 4 inch.

Someone has any idea about a solution ?

Thanks by advance.

Sylvain.

回答1:

Some points in providing several layouts

  • Dont distinguish resource folders according to specific devices. The less folders you make, more the better...
  • Compare devices with their resolutions too when comparing with their sizes(One e.g is Nexus 7. Nexus 7 have a resolution of 1280 * 800 and other similar 7 inch tablets like Samsung Galaxy Tab have resolution of 1024 * 600).

The better practice is to put the following drawables

// for Phones
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi

//for 7 inch tablets
drawable-large-mdpi
drawable-large-hdpi(for Nexus 7)

// for 10 inch tablets
drawable-xlarge-mdpi

Giving support to tablets in Android is a hell job IMO. Because there are so many devices and each have their own resolutions and densities. You can't target all the devices.

Nexus 7 case: Even though Nexus 7 is a 7 inch tablet, it has resolution of 1280 * 800. So it's an hdpi device. But normal 7 inch devices have lower resolutions of 1024 * 600. So they are mdpi devices and the drawable qualifier can change. (From my own experience, first put a folder drawable-large-mdpi for 7 inch devices and check it on Nexus 7. If there is no problem with images, you dont have to put another folder. Because if a particular folder is not present, Android will check for the nearest possible folder and optimize it for the device screen).

Now the problem with giving layout-sw<>dp attribute are

  • Only added in API Level 13. So can't support earlier devices.
  • Because of its high precedence in qualifier list, we can't give other layouts starting with lower precedence qualifiers. Because Android always take higher precedence attributes first.


回答2:

Last (and valuable) hint:

Organize your layouts by SIZE, never DENSITY. Use dp and sp units, never pixels.

Organize your drawables by DENSITY, never SIZE. If you use 9-pacthes, provide alternative densities to mdpi, hdpi, xhdpi, xxhdpi too.