开发者

Android: Incorrect size for views with mm or inch dimensions

开发者 https://www.devze.com 2023-03-10 16:19 出处:网络
I\'ve got a button having a layout_height of 10mm which loo开发者_Python百科ks correct on most devices, but on for instance the Lg Optimus 2X and on the Motorola Defy the buttons have about half the s

I've got a button having a layout_height of 10mm which loo开发者_Python百科ks correct on most devices, but on for instance the Lg Optimus 2X and on the Motorola Defy the buttons have about half the size:

i retrieved the DisplayMetrics info on those devices and here's a short overview:

HTC Desire Z:       480x800, density : HIGH, xdpi: 254.0, ydpi: 254.0 
Motorola Defy:     480x854, density : HIGH, xdpi: 96.0, ydpi: 96.0
Samsung Galaxy S2: 480x800, density : HIGH, xdpi: 217.71428, ydpi: 218.49463 
LG Optimus 2X:     480x800, density : HIGH, xdpi: 160.0, ydpi: 160.0

As you can see the Desire Z and the Samsung have a reasonable looking x/y-dpi value. the defy and the optimus deliver obsiously incorrect values that I assume are the reason for the wrong size.

Is there any other way to get around that hardware bug except creating seperate layout files for those devices?

Or is it recommended not using mm/in sizes at all?


Basted on the additional information you provided in Kristiono Setyadi's answer I say you should probably set the size of the button to 48 dp. The dp unit takes the dpi of the device into account so things specified using dp will appear to have similar physical size across devices of different resolution and dpi.

One good thing with the dp unit is that is not effected by the hardware bug you have noticed since it's not based on xdpi/ydpi but instead densityDpi which seems to be implemented more or less correctly on all devices.

In case other people with similar problems find this question I'll try to answer your other questions even though using dp should be good enough for you.

Is there any other way to get around that hardware bug except creating seperate layout files for those devices?

You should not create separate layout files for those devices, because there are more devices out there that are broken in the same way. Galaxy Mini and Galaxy S3 Mini are two other phones I've found that behaves the same way.

Or is it recommended not using mm/in sizes at all?

You can use mm/in if you have something that really needs to be a specific number of mm or inch. In this case using dp will not work on all devices since number-of-dp-per-inch varies between devices. For more details on this please see: Android: which UI element size unit to use (if dp not working)

When using mm/in you have to be careful about "broken" devices that reports wrong values for xdpi / ydpi.

One way to work around it could be to use mm or inch for devices where DisplayMetrics.xdpi / DisplayMetrics.densityDpi is larger than 0.8 but smaller than 1.2 and stick with dp for other devices since devices with that big difference between densityDpi and xdpi probably reports incorrect values for xdpi.

For more details on the available units and the problems with mm and inch please see: Why Lint shows warning when using in (inch) or mm (millimeter) units as dimension?


You should not use mm/in size to declare the dimension value of the UI. It is not recommended. Why? Because it will not scale correctly across devices.

The recommended way to do that is using the dp/sp to measure view object/font size.

Read this article:

More Resource Types - Dimension

0

精彩评论

暂无评论...
验证码 换一张
取 消