简体   繁体   English

Android对支持多种屏幕尺寸的困惑

[英]Android Confusion on supporting multiple screen sizes

Ive read Android documentation on the subject of supporting multiple screen sizes but I just cant seem to wrap my head around it. 我已经阅读了有关支持多种屏幕尺寸的主题的Android文档,但我似乎无法解决这个问题。 I know qualifiers need to be set in layout names and android picks the one whos qualifier is closes to the width of the phone. 我知道必须在布局名称中设置限定词,而android会选择谁的限定词接近手机的宽度。

Im not to worried about tablets at the moment but when I develop for my physical device everything is fine and dandy all in default layout folder: layout/main.xml 我暂时不担心平板电脑,但是当我为自己的物理设备开发时,一切都很好并且很漂亮,都位于默认的布局文件夹中:layout / main.xml

But if I test on my friends phone Samsung Galaxy S4 everything is streched in a vertical way. 但是,如果我在朋友手机Samsung Galaxy S4上进行测试,那么一切都是垂直的。 I just dont know where to begin really... 我只是不知道从哪里真正开始...

Is there a standard set of folders that I could implement to lay my layouts in and then edit for optimized performance? 是否可以实现一组标准文件夹来放置布局,然后进行编辑以优化性能?

http://developer.android.com/training/basics/supporting-devices/screens.html . http://developer.android.com/training/basics/supporting-devices/screens.html It's all explained in this tutorial link. 在本教程链接中对所有内容进行了说明。

If one layout should support all screens then, 如果一种布局应支持所有屏幕,

  1. your app theame should be similer for tabs and mobiles 您的应用程式theame应该适合标签页和手机

  2. never use fixed values for layouts like (ex:300dp , 15dp), all are wrap , match , fill depends on requirement, 切勿对(ex:300dp,15dp)之类的布局使用固定值,它们都是wrap,match,fill取决于要求,

  3. make all the icons, images in 2 or 4 different sizes 使所有图标,图像具有2或4种不同的尺寸

NOTE: if you have minor changes for smaller to larger device ui, then in on create get the device height and width , make your changes (images, layout widths etc..) runtime from java 注意:如果从较小到较大的设备ui有较小的更改,则在create上获取设备的高度和宽度,然后从Java运行时进行更改(图像,布局宽度等)。

If your app is not similer from small to big devices 如果您的应用程序不适合从小型设备到大型设备

Use layout(mobiles) ,layout-large(7 inch tabs) layout-xlarge(10 inch tabs) 使用布局(手机),布局大(7英寸制表符)layout-xlarge(10英寸制表符)

Start with device independent pixels (dp) and scalable-independent pixels for fonts (sp) 从与设备无关的像素(dp)和与字体无关的可缩放像素开始(sp)

If something is stretched vertically after that, it probably means it's a background image that's trying to fill up the entire height of a layout (by the way, next time you have a problem, please do post the relevant xml code and dimensions of the image, or at least a screenshot of the problem). 如果此后垂直拉伸,则可能是背景图片试图填充布局的整个高度(顺便说一句,下次遇到问题时,请发布相关的xml代码和图片尺寸) ,或者至少是问题的屏幕截图)。

In that case, just use the different size qualifiers to solve your problem ( not your density qualifiers). 在这种情况下,只需使用不同大小的限定词即可解决您的问题( 而不是密度限定词)。 Density qualifiers don't help for widths or heights of bitmaps that are larger than the widths or the heights of device screens. 密度限定符对于位图的宽度或高度大于设备屏幕的宽度或高度没有帮助。 This is just a rule of thumb that you should be aware of. 这只是您应注意的经验法则。

Android Studio布局设计器的屏幕截图,比较了不同屏幕类型的相同布局

Use the toolbar select box in the layout designer of Android Studio/IntelliJ to quickly test multiple device screens all in one go. 使用Android Studio / IntelliJ的布局设计器中的工具栏选择框,一次即可快速测试多个设备屏幕。 This is the fastest way to do it. 这是最快的方法。 Do not use Eclipse for that, its ADT layout designer doesn't have that capability (at least, not the last time I've checked). 请勿为此使用Eclipse,它的ADT布局设计器不具备此功能(至少不是我上次检查的能力)。 Even if you use Eclipse most of the time, it's worth using Android Studio/intelliJ just for that functionality, and then switching back to Eclipse (assuming it's still your favorite IDE after that) when you're done with testing that aspect of the layout. 即使您大部分时间使用Eclipse,也值得仅将Android Studio / intelliJ用于该功能,然后在测试完布局的那一部分后再切换回Eclipse(假设在那之后它仍然是您最喜欢的IDE)。 。

Also, don't try to do a different layout for each size qualifier that you encounter. 另外,不要尝试对遇到的每个尺寸限定符进行不同的布局。 Only use different size qualifiers for the layouts that are actually giving you problems. 仅对实际给您带来问题的布局使用不同的大小限定符。 And don't forget that common layout components that don't need to be duplicated can just be abstracted away in a parent layout. 并且不要忘记,不需要重复的通用布局组件可以在父布局中抽象出来。 And if you can find a way to resolve your specific problem without the use of size qualifiers, that can work just as well. 而且,如果您找到一种无需使用大小限定符即可解决特定问题的方法,那么效果也很好。 For instance, if instead of using a single image for your background, you could replace it with a larger image that you don't mind getting clipped at different aspect ratios, or replace the background image with something entirely different and abstract (like a solid color, a gradient, a tiling background, a large vector graphic, or a large patch-nine png) that looks ok at different aspect ratios, that could be even simpler still. 例如,如果您不使用单个图像作为背景,而是可以将其替换为您不介意以不同的长宽比裁剪的较大图像,或者将背景图像替换为完全不同的抽象图像(例如纯色颜色,渐变,平铺背景,大矢量图形或大的贴图九png)(在不同的宽高比下看起来都不错),甚至可能更简单。

At the high level, you just need to take care of these things: 在较高级别,您只需要注意以下事项:

- Better have the images sliced for either XHDPI (720x1280) OR XXHDPI (1080x1920) resolution and keep them in respective folders. -最好将图像切片为XHDPI (720x1280)或XXHDPI (1080x1920)分辨率,并将其保存在相应的文件夹中。 XHDPI images into drawable-xhdpi and XXHDPI images into drawable-xxhdpi folder. XHDPI图像放入drawable-xhdpiXXHDPI图像放入drawable-xxhdpi文件夹。 One set of image slices are enough. 一组图像切片就足够了。
- You don't need to write different XML layouts for the same screen if you are not supporting Tablets (except for very rare times) -如果您不支持平板电脑,则无需为同一屏幕编写不同的XML布局(极少数情况除外)
- Try to avoid hard coding the android:layout_width & android:layout_height parameters. -尝试避免对android:layout_widthandroid:layout_height参数进行硬编码。 Use wrap_content , fill_parent OR match_parent . 使用wrap_contentfill_parentmatch_parent
- Try to avoid keeping any image slices inside simple drawable folder instead keep inside specific drawable folders like drawable-xhdpi , drawable-hdpi etc. -尽量避免将任何图像切片保留在简单的drawable文件夹中,而应保留在特定的drawable文件夹中,例如drawable-xhdpidrawable-hdpi等。
- Try to use, color codes & 9 patch images as much as possible instead of using images for everything which will reduce the build size and also helps in avoiding OutOfMemoryException . -尝试使用尽可能多的颜色代码和9个补丁图像,而不是对所有内容使用图像,这将减少构建大小并有助于避免OutOfMemoryException
- Inside simple drawable folder, keep all your XML drawables like background of a button with separate images for clicked & focused states etc. -在简单的drawable文件夹中,保留所有XML drawable,例如按钮的背景,以及用于单击和聚焦状态等的单独图像。

If you follow these steps, you don't need to worry about supporting multiple screen resolutions for most of the cases. 如果执行这些步骤,则在大多数情况下无需担心支持多种屏幕分辨率。

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

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