Android中特定于密度的布局与特定于尺寸的布局 [英] Density-specific layouts vs. size-specific layouts in Android

查看:51
本文介绍了Android中特定于密度的布局与特定于尺寸的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直在声明用于4种不同密度的资源文件夹:

For quite a long time, I have been declaring resource folders for 4 different densities:

  • drawable-ldpi
  • drawable-mdpi
  • drawable-hdpi
  • drawable-xhdpi

在布局的XML中,我一直在使用固定宽度(尽管仍与密度无关),例如这些图形使用128dp.

In the layout's XML, I have been using fixed widths (while still density-independent) such as 128dp for those graphics.

但是,当越来越多的大屏幕手机(尤其是平板电脑)问世时,这种方法就不起作用了.尽管您通过这种方式提供了与密度无关的资源,但是在大屏幕上布局看起来并不理想.

However, when more and more large-screen phones, and especially tablets, were introduced, that approach did not work anymore. Although you provide density-independent resources this way, the layout will not look good on large screens.

这就是为什么我认为我需要添加依赖于屏幕大小的Dimension资源,以用于XML布局,例如:

This is why I think I need to add Dimension resources that depend on the screen size, for use in the XML layouts, e.g.:

  • values-w600dp
  • values-w720dp
  • values-w1024dp

但这是否意味着我应该放弃对这4个密度容器的支持?还是我需要提供16个资源文件夹,即密度和大小的每种组合都提供一个?

But does that mean that I should drop supporting those 4 density containers? Or do I need to provide 16 resource folders, i.e. one for every combination of density and size?

在关于此主题的 Android文档中找不到任何好的帮助.

I can't find any good help in the Android documentation as to this topic.

推荐答案

可绘制对象和布局不同.要回答您的问题,您是否应该停止支持这些密度.是的,但是您仍然应该支持xdpi和hdpi.罗曼·盖伊(Romain Guy)最近说,像Nexus 7这样的现代设备(在tvpi上)可以适当扩展资产,以至于真正不需要mdpi.而且没有人再使用ldpi了.我最后看的是不到2%的市场.

drawables and layouts are different. To answer your question, should you stop support those densities. Yes, but you should still support xdpi and hdpi. Romain Guy recently said that modern devices like the Nexus 7 (at a tvpi) can scale the assets properly enough that mdpi isn't really needed. And nobody uses ldpi anymore. Last I looked is was less than 2% of the market.

关于布局. Nexus 7(1280x800 tvdpi)会使用值-w1024dp中的内容,但仍会从drawable-hdpi文件夹中获取资产.这两个不是互斥的.像S3之类的东西也会从值-w1024dp中拉出,但使用drawable-xdpi.仅在用例需要时才需要提供替代布局.

About layouts. A Nexus 7 (1280x800 tvdpi) would use something from the values-w1024dp but still get assets from the drawable-hdpi folder. Those two aren't mutually exclusive. Something like a S3 would also pull from the values-w1024dp but use drawable-xdpi. You only need to provide an alternative layout if your use-case calls for it.

那么您需要16种不同的东西吗?否.您确实需要xdpi& hdpi(如果不是mdpi).您可能希望包含不同尺寸的替代布局.您可以根据自己的需要指定特定的名称,也可以选择一般的名称.除非您同时为电话和电话都做一个混合应用程序平板电脑(7英寸和10英寸),您可能不需要很多xxxx-sizexxx文件夹.

So do you need 16 different things? No. You do need xdpi & hdpi (if not mdpi). You may want to include alternative layouts for different sizes. You can be as specific as you want or as generic. Unless you're doing a hybrid app for both phone & tablet (7 & 10 in) you probably don't need a lot of xxxx-sizexxx folders.

在布局的XML中,我一直在为这些图形使用固定宽度(尽管仍然与密度无关),例如128dp.

In the layout's XML, I have been using fixed widths (while still density-independent) such as 128dp for those graphics.

这可能是您遇到问题的根源.您的布局应使用wrap_content和match_parent尽可能流畅.应当保留固定的尺寸,以便提前在您知道尺寸的侧面和图像周围进行填充.如果这样做,从小到320 x 200到GTV的任何尺寸,布局都应该看起来不错.

This is probably a source of your issues. Your layouts should be as fluid as possible using wrap_content and match_parent. Fixed sizes should be reserved for padding around the sides and image where you know the size ahead of time. If you do this, your layout should look decent at any size from a small 320 x 200 to a GTV size.

这篇关于Android中特定于密度的布局与特定于尺寸的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆