android layout at different screen sizes

2019-04-30 10:02发布

问题:

trying to get my head around screen sizes so my app looks the same on different devices.

I have two devices a galaxy s3 and a galaxy tab 8. both have the same resolution screen 720 x 1280 but the s3 is 4.7 inches while the tab is 8 inches

When I do a linear layout I set padding to 130 at the top so that the first textview is 130 pixels from the top so the image on the background is not obscured.

This is fine on the s3 but when I load it on the tab the padding needs changing to 190 pixels for it to look the same.

I cant get my head round if the height of the screen resolution is the same (1280) on both devices why do i need larger padding on the screen

is there a way of working out how to make the textview appear on the same location on both devices

Any help appreciated

Mark

回答1:

Yes there is a way.

First off all you should use DP or SP unit instead of PX. The DP unit have in consideration the size of the screen and the screen resolution. You can see more in here:

What is the difference between "px", "dp", "dip" and "sp" on Android?

Also you can create a folder in res and add different sizes for different devices.

Example:

You already have the folder 'values' with the file 'dimens.xml' in there you can add margins and sizes variables.

If you create the folder 'values-large' and copie the file 'dimens.xml', you can change the sizes of the variables and maintain the name.

In devices 'large' it will load different values from the rest of the devices.

You can see all documentation in here: http://developer.android.com/guide/practices/screens_support.html

Hope it helps you.



回答2:

You need to add 2 different layouts for 2 different screen sizes. In fact you can provide multiple screen support limited only by your considerations. You can customize layout according to the screen sizes. Here is the official documentation to help you get going.



回答3:

First of all, you need to use dp instead of px to do these kind of stuff. The reason the result is wrong is that the number of dots per inch on the s3 is much higher than that of the galaxy tab 8. You should really read this link to understand more.

[http://www.captechconsulting.com/blog/steven-byle/understanding-density-independence-android][1]