简体   繁体   English

计算不同屏幕的dp以支持多个屏幕

[英]calculate dp for different screen for supporting multiple screens

I know there are many questions like this, I have read many blogs and questions on this and I am not satisfied by what I have understand. 我知道有很多这样的问题,我读过许多博客和与此相关的问题,我对所了解的内容不满意。

I want to support multiple screens and resolutions like hdpi, xxxhdpi etc. And i am using dimens.xml for this purpose. 我想支持多个屏幕和分辨率,例如hdpi,xxxhdpi等。为此,我正在使用dimens.xml But m still unclear about how to calculate exact dp value for different screens. 但是我仍然不清楚如何计算不同屏幕的精确dp值。

For example if 48dp x 48dp ImageView is proper for hdpi device, what values should i define for xxhdpi device? 例如,如果48dx x 48dp ImageView适用于hdpi设备,那么我应该为xxhdpi设备定义哪些值? Is there any fixed calulation just like there is for drawable images? 是否有像绘制图像一样的固定计算?

I am using these folder for trying to support multiple screens : 我正在使用这些文件夹尝试支持多个屏幕:

values-sw320dp-hdpi
values-sw320dp-xhdpi/xxhdpi/xxxhdpi

There are few problems I am facing currently 我目前面临的问题很少

  • Though defining values in respective dimens.xml works almost for most of the devices, there are few devices which takes wrong values 尽管在各自的dimens.xml中定义值几乎适用于大多数设备,但很少有设备会采用错误的值

For example I have Lg optimus G that is xhdpi device, but it reads values from values-sw320dp-xxxhdpi 's dimens.xml instead of xhdpi one 例如,我有Lg optimus G,它是xhdpi设备,但它从values-sw320dp-xxxhdpidimens.xml读取值,而不是从values-sw320dp-xxxhdpi读取值

  • I am not able to calculate the exact value for each resolution (hdpi,xhdpi etc), so the view which looks perfect in hdpi device, seems little large or small in xxxhdpi as I can't guess the value like if hdpi view size is 48dp then xxxhdpi should be of 64dp or something as I don't know the exact approach. 我无法计算每种分辨率(hdpi,xhdpi等)的确切值,因此在hdpi设备中看起来很完美的视图在xxxhdpi中看起来有点大或小,因为我无法猜测像hdpi视图大小是48dp,然后xxxhdpi应该是64dp或其他,因为我不知道确切的方法。

Also while searching for supporting different screens, I read many times about calculating dp at runtime based on density or calulating pixels etc. 另外,在搜索支持不同屏幕的过程中,我阅读了很多有关在运行时基于密度或计算像素等来计算dp的信息。

I am too confused about all this. 我对这一切感到困惑。 Please help me in understanding and learning the proper way of making responsive apps. 请帮助我理解和学习制作响应式应用程序的正确方法。

You can use resource qualifiers to specify different sizes for Views. 您可以使用资源限定符为视图指定不同的大小。

If you want the width of an ImageView to vary based on the size of the screen, create a new dimension in your dimens.xml file called image_view_width (for example). 如果您希望ImageView的宽度根据屏幕的大小而变化,请在您的dimens.xml文件中创建一个新的尺寸,例如image_view_width

This then essentially creates a copy of the dimens.xml but adding the 'Size' qualifier. 然后,这实际上创建了dimens.xml的副本,但添加了“大小”限定符。 This will create another version of the dimens.xml file, and any device that meets your specified qualifications will use that version of dimens.xml . 这将创建另一个版本的dimens.xml文件,任何符合您指定资格的设备都将使用该版本的dimens.xml You then add image_view_width to this new file and give it a different (smaller/bigger) value. 然后,您将image_view_width添加到此新文件,并为其提供不同的值(较小/较大)。 You can do this as many times as you want and with as many different qualifiers as you want. 您可以根据需要进行多次,也可以根据需要使用不同的限定词。

Finally, when you want to use this value in your layouts, you only have to type: 最后,当您想在布局中使用此值时,只需键入:

android:layout_width="@dimen/image_view_width"

and android will do the rest for you. 而android将为您完成其余的工作。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM