开发者

Android: 50dip is different size on different devices?

开发者 https://www.devze.com 2023-03-04 22:38 出处:网络
My understanding of the density indep开发者_StackOverflow中文版endent pixel, is that it will render as the same size regardless of the screen dpi and dimensions.I created an image button of with and h

My understanding of the density indep开发者_StackOverflow中文版endent pixel, is that it will render as the same size regardless of the screen dpi and dimensions. I created an image button of with and height of 50dip. On my medium dpi Samsung Galaxy Tab, this button measures about 10mm x 10mm (measured with a ruler. On my large, hdpi Acer Iconia Tab, the button measures about 7mm x 7mm. I would have expected the two buttons to have the same actual size. Am I doing something wrong, or is my understanding incorrect?


Both previous answers are correct. The problem lies in that not only changes the size in inces, but also the number of total pixels.

For a solution, you will need to create different xml layout files in res/layout-normal and res/layout-large, to adjust for screen resolution changes. Still, given that these folders group ranges of devices, you will have some variation in size.

Alternatively, if you strictly need a specific fixed size for a View, set up its width and height as "10mm"


public static final int layout_width               Since: API Level 1

Specifies the basic width of the view. This is a required attribute for any view inside of a containing layout manager. Its value may be a dimension (such as "12dip") for a constant width or one of the special constants.

May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

http://developer.android.com/reference/android/R.attr.html#layout_width


For a list of screen sizes, resolutions and dpi values, take a look at: http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density

To calculate the real dpi value, check here: http://en.wikipedia.org/wiki/Pixel_density#Calculation_of_monitor_PPI


You've got it the wrong way round, the Galaxy Tab is HDPI and the Acer Tab is MDPI.
50dips is 50 pixels on an MDPI device or 75 pixels on an HDPI device, which should translate to roughly the same physical size.

However things are slightly different due to the much larger screen size on 3.0 tablets. In these cases often using the HDPI assets gives a better size.


from: http://developer.android.com/guide/practices/screens_support.html (emphasis added)

Density-independent pixel (dp) A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

0

精彩评论

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