Same DPI but different inch size

北城余情 提交于 2019-12-05 18:21:14

This might help explain the problem you are facing...

You have an image that is 250x125 - that is 250 pixels wide by 125 pixels in height.

You have specified 160 dpi - which means that 1 inch = 160 pixels.

So, both screens are doing what you ask and displaying the 250 pixels across 1.5625 inches. On the large screen it looks "proportionally" correct. On the 3.65" screen the button takes up more than half the screen - just like you asked it to.

If you want the smaller screen to look like the larger screen, then you have three options:

  1. adjust the size of the image and provide 2 image assets (or more for a wider variety of screens). This is why you can have resource folders for mdpi, hdpi, xhdpi, etc. You adjust the pixels in the image to accommodate the screen size.

  2. You use a "weighted" LinearLayout that adjusts the size of the space provided based on the available screen space. For this type of layout you should not worry about performance.

  3. Do runtime scaling of the image based on screen size - use DisplayMetrics to get the size and density of the screen and adjust your image to fit the screen appropriately.

The last option is the most flexible in many ways, because if you end up with a screen that is either very large or very small, you can make adjustments to do things like move buttons or text to another place on the screen (above or below other content). But for your specific problem, any of them will suffice.

There is no need of Designing two xml layout.

You can use Dimension for margin and padding according to device.

You are giving static value for margin.

Use dimen.xml in value folder each device.

Following code in your layout.xml will work for you.

android:layout_marginLeft="@dimen/margin_button"

Value folder name for dimen.xml:

values-mdpi
values-hdpi
values-xhdpi
values-xxhdpi
values-sw600dp

create dimen.xml in each values folder.

and in dimen.xml you have to define value for margin in all values folder but value of that property is different according to device size like this:

values-mdpi

<dimen name="margin_button">20dp</dimen>

values-hdpi

<dimen name="margin_button">23dp</dimen>

like wise in all values folders.

Thanx everyone for the answers. Due to answer from @Iacs I discovered that I had to made changes to my folder structure.

I have completely overlooked the fact that in the /res folder there can be more directories then just the standard "layout" directory. You can create other directories with these names : layout-large, layout-xlarge, layout-small, and so on... In these folders you can paste your layout.xml and adjust the values...

This is how things look now in my android studio note the layout folder structure:

And now ofcourse my 2 devices with both the same DPI but different screen size are showing my buttons the way I want them to be showned!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!