简体   繁体   English

如何为多种Android屏幕尺寸制作布局?

[英]How do you make layouts for several Android screen sizes?

I've done some research on building layouts that work for multiple screen sizes and I'm looking for some clarification. 我已经做了一些关于构建适用于多种屏幕尺寸的布局的研究,我正在寻找一些澄清。

Is it common practice to just make a separate layout file for each of the three screen sizes (small, medium, large) or can you accomplish this with an easier method? 通常的做法是为三种屏幕尺寸(小,中,大)中的每一种制作单独的布局文件,还是可以通过更简单的方法完成此操作?

I've been testing my projects on a large screen device, and even though I use DIPs (density independent pixels) for padding, margins, etc, it still scrunches stuff up when I view it on smaller screens. 我一直在大屏幕设备上测试我的项目,即使我使用DIP(密度无关像素)进行填充,边距等,当我在较小的屏幕上查看时,它仍然会重新填充。 Should I be designing my projects for medium-sized screens and then just allow Android to scale it appropriately? 我应该为中型屏幕设计我的项目,然后让Android适当地扩展它吗?

I'm not sure if this is a good question or not, but I am looking for what the common practice is for designing for multiple screen sizes. 我不确定这是不是一个好问题,但我正在寻找设计多种屏幕尺寸的常见做法。 What do you do? 你是做什么?

Edit: In addition to this, for example, let's say I have a button that is 40dip above the bottom of the screen, should I literally write 40dip, or should I be using some sort of pixel math like 40 * screenWidth / blahblah or something so that it scales depending on the screen size the user has? 编辑:除此之外,例如,假设我有一个位于屏幕底部40dip的按钮,我应该写40dip,还是应该使用像40 * screenWidth / blahblah之类的像素数学这样根据用户的屏幕尺寸进行缩放? I have limited experience with UIs... 我对UI的经验有限......

There are two axes to consider when it comes to screen size: physical size and density. 屏幕尺寸有两个轴需要考虑:物理尺寸和密度。 Density is handled by providing measurements in dips and scaled resources as appropriate. 密度通过提供适当的下降和缩放资源的测量来处理。 But density does not always imply size or vice versa. 但密度并不总是意味着大小,反之亦然。 See http://developer.android.com/guide/practices/screens_support.html for some further info on the mechanics. 有关机制的更多信息,请参见http://developer.android.com/guide/practices/screens_support.html

It is not common or recommended to have different layouts based on each screen resolution you support, but it is entirely reasonable to design different layouts for different size classes (small, medium, large). 根据您支持的每个屏幕分辨率 ,不常见或建议使用不同的布局,但为不同的大小类(小,中,大)设计不同的布局是完全合理的。 Different sized screens might benefit from adding, removing, or repositioning certain navigation elements depending on the app. 根据应用程序的不同,添加,删除或重新定位某些导航元素可能会使不同大小的屏幕受益。

Within a certain size class you should make sure that your layouts tolerate variances in exact screen resolution. 在特定大小的类中,您应该确保您的布局能够容忍精确屏幕分辨率的差异。 As Falmarri suggested, use relative layouts, weights, and the other tools available to let your layout stretch gracefully. 正如Falmarri建议的那样,使用相对布局,权重和其他可用工具让您的布局优雅地伸展。

The general rule is to use Density Independent Pixels (dips) for size definitions in your layout xmls - I see you already do it. 一般规则是在布局xmls中使用密度无关像素(凹陷)进行大小定义 - 我看你已经这样做了。 Doing so I just have the only layout for all range of devices. 这样做我只有所有设备的唯一布局。 What needs to be splited is graphics. 需要拆分的是图形。 For that I use 3 different drawable directories - 'drawable-ldpi', 'drawable-mdpi' and 'drawable-hdpi'. 为此,我使用3个不同的可绘制目录 - 'drawable-ldpi','drawable-mdpi'和'drawable-hdpi'。 This is done in order images to have the same size (let say, in millimeters) on various screen densities (assuming screen size is the same - Normal, for instance) they should be scaled as follows: 这样做是为了使图像在各种屏幕密度上具有相同的尺寸(例如,以毫米为单位)(假设屏幕尺寸相同 - 例如,正常),它们应按比例缩放:

  • drawable-hdpi: 150% drawable-hdpi:150%
  • drawable-mdpi: 100% drawable-mdpi:100%
  • drawable-ldpi: 75% drawable-ldpi:75%

Probably it is a bad advice. 可能这是一个不好的建议。 However, if you look at the Google chart of Screen Sizes and Densities you can decide to not invest your additional efforts to thorough testing for Large screens, since there are almost no such devices on the market. 但是,如果您查看屏幕尺寸和密度Google图表,您可以决定不再投入额外的精力来对大屏幕进行全面测试,因为市场上几乎没有这样的设备。

No making separate layouts is not really common practice. 没有制作单独的布局并不是一种常见的做法。 Only when you have images that can't be stretched is that really the recommended way. 只有当您拥有无法拉伸的图像时才是真正推荐的方式。

Stuff will always look a little stretched/compressed when viewing it on devices with smaller/larger screens. 在具有更小/更大屏幕的设备上查看时,东西看起来总是看起来有点拉伸/压缩。 That's kind of the definition of a different size screen. 这就是不同大小屏幕的定义。 You should just use relative layouts and let android control the specific pixel numbers. 你应该只使用相对布局,让android控制特定的像素数。

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

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