简体   繁体   English

支持Android的不同屏幕

[英]Supporting different screens for android

This is my first time working with multiple screens. 这是我第一次使用多个屏幕。 I want to build my application for multiple screens ie from sw320dp to sw720dp . 我想为多个屏幕构建应用程序,即从sw320dpsw720dp I have created the following layout folders. 我创建了以下布局文件夹。

res/layout-sw320dp
res/layout-sw360dp
res/layout-sw480dp
res/layout-sw600dp
res/layout-sw720dp

I have copied all the xml files inside these folders. 我已经复制了这些文件夹中的所有xml文件。 Is there anything else I need to add to make sure all the layouts support multiple screens. 还有什么我需要添加的,以确保所有布局都支持多个屏幕。 I have gone through the android documentation but I am not clear with the manifest.xml part. 我已经阅读了android文档,但对于manifest.xml部分不清楚。 If anyone implemented multiple screen support in their application, so please do provide a description and implementation of the same. 如果有人在其应用程序中实现了多屏支持,那么请提供其描述和实现。

Step 1 -You have to create different values folder for Different values for different screens. 步骤1-您必须为不同屏幕的不同值创建不同的值文件夹。 Go to Your Project / app / src / main / res. 转到您的项目/应用程序/ src / main / res。 Right click on res and create different values folder in it. 右键单击res并在其中创建其他值文件夹。

Step - 2. Create folders named values-large values-small values-sw320dp values-sw320dp-hdpi values-sw320dp-xhdpi values-sw320dp-xxhdpi values-sw480dp values-sw600dp values-sw720dp Step - 3. Create dimensions.xml file in values folders. 步骤-2.创建名为以下值的文件夹-大值-小值-sw320dp值-sw320dp -hdpi值-sw320dp -xhdpi值-sw320dp-xxhdpi值-sw480dp值-sw600dp值-sw720dp步骤-3.在以下位置创建Dimensions.xml文件值文件夹。 Different values for different screen size. 不同屏幕尺寸的不同值。

values-ldpi                     2.7" 240*320 ldpi
values-ldpi                     3.3" 240*400 ldpi
values-ldpi                     3.4" 240*432 ldpi

values-mdpi                     3.2" 320*480 mdpi

values-hdpi                     4.0" 480*800 hdpi
values-hdpi                     3.7" 480*854 hdpi

values-xhdpi                    4.7" 1280*720 xhdpi
values-xhdpi                    4.65" 720*1280 xhdpi

values-sw480dp                  5.1" 480*800 mdpi
values-sw480dp                  5.4" 480*854 mdpi

values-sw600dp                  7.0" tablet 1024*600 mdpi

values-sw720dp                  10.1" tablet 1280*800 mdpi

when you attach dimension.xml file with your layout than you will get direct effect with your screen size. 当您将Dimension.xml文件附加到布局中时,屏幕尺寸将直接产生影响。 This will help you to set dimensions for all type of screens. 这将帮助您设置所有类型屏幕的尺寸。

There is a difference between supporting multiple screen sizes and creating different layout.xml files for each screen size. 支持多种屏幕尺寸和为每种屏幕尺寸创建不同的layout.xml文件之间是有区别的。

In all the apps I've ever worked on, there were really only three different kinds of screens we cared about: small phones (years-old devices that our users weren't upgrading), "regular" phones (eg modern-day Samsung or LG phones etc), and tablets. 在我曾经使用过的所有应用程序中,我们实际上只关心三种不同类型的屏幕:小型电话(用户未升级的使用多年的设备),“常规”电话(例如现代三星)或LG手机等)和平板电脑。 Even considering those three kinds of screens, we often didn't need to create more than a single layout.xml file for a single screen. 即使考虑到这三种屏幕,我们通常也不需要为单个屏幕创建多个单个layout.xml文件。

If you have just one layout.xml file, it will display itself on any screen size. 如果只有一个layout.xml文件,它将在任何屏幕尺寸上显示其自身。 To "support" multiple screen sizes, you just need to make sure that your content looks good on short phones and tall phones, on wide phones and narrow phones, on phones and tablets, etc. This generally comes down to using dimensions like match_parent , or layout_weight to fill available space, etc. 要“支持”多种屏幕尺寸,您只需要确保您的内容在短电话和高电话,宽电话和窄电话,电话和平板电脑等上看起来都不错。通常归结为使用match_parent尺寸,或layout_weight填充可用空间等。

It is only when you actually need to change what elements are on screen (as opposed to how big elements are) that you need to create extra layout.xml files. 仅当您实际需要更改屏幕上的元素 (而不是元素)时,才需要创建额外的layout.xml文件。 For instance, perhaps you know that a certain set of text + images just won't fit on smaller phones. 例如,也许您知道,某些文本+图片集仅适合较小的手机。 Then you can create one res/layout/layout.xml that has only the text, and another res/layout-sw360dp/layout.xml that has the text + the image. 然后,您可以创建一个仅包含文本的res/layout/layout.xml ,以及另一个包含文本+图片的res/layout-sw360dp/layout.xml Or maybe you have some content that you want to display side-by-side on a tablet, but you only want part of it on phones. 或者,也许您有一些要在平板电脑上并排显示的内容,但只希望部分内容在手机上显示。 Then you can make one res/layout/layout.xml with the normal content and one res/layout-sw600dp/layout.xml with the tablet-only content. 然后,您可以使用常规内容制作一个res/layout/layout.xml使用仅限平板电脑的内容制作一个res/layout-sw600dp/layout.xml

Regardless, when you decide that you do want to make multiple versions of a layout for different screen sizes, the only thing you have to do is create copies of your layout.xml in different layout-swXXXdp folders. 无论如何,当你决定, 想为不同的屏幕尺寸布局的多个版本,你必须做的唯一事情是创建在不同的layout.xml副本layout-swXXXdp文件夹。 Don't bother with layout-large unless your app supports really old API levels; 除非您的应用程序支持真正的旧API级别,否则不要layout-large the swXXXdp method is much more accurate and solves the same problem (but was only added in API 13). swXXXdp方法更加准确,可以解决相同的问题(但仅在API 13中添加)。

Hey you dont need to do anything in manifest. 嘿,您不需要做任何明显的事情。 You have done the part with layouts. 您已经完成了布局部分。 Next you can do is to add support in drawable folder ie different density images for different sizes. 接下来,您可以在可绘制文件夹中添加支持,即针对不同大小的不同密度图像。

And if different screens require different values(dimentions etc) you need to create multiple file in values. 如果不同的屏幕需要不同的值(尺寸等),则需要创建多个值的文件。

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

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