简体   繁体   English

在Android中支持不同的屏幕尺寸

[英]Support different screen sizes in Android

I am getting very confused on how to support all different Android screen sizes. 我对如何支持所有不同的Android屏幕尺寸感到非常困惑。 I have already checked this answer , this site , the official documentations and many other answers but still I am confused so please don't mark this as Duplicate . 我已经检查了这个答案 ,这个网站 ,官方文档和许多其他答案,但是我仍然很困惑, 所以请不要将此标记为重复

I already created layout-sw120dp, layout-sw160dp, layout-sw240dp, layout-sw320dp, layout-sw480dp (I only need to support up to 6 inches so I don't create layout-sw600dp ). 我已经创建了layout-sw120dp, layout-sw160dp, layout-sw240dp, layout-sw320dp, layout-sw480dp (我只需要支持6英寸,所以我不创建layout-sw600dp )。

What do I have to do is just have one layout and just copy it in each different folder so Android will do the selection on its own or I also need to change values in each layout? 我需要做的就是只有一个布局,然后将其复制到每个不同的文件夹中,以便Android自行进行选择,或者我还需要更改每个布局中的值?

What do I have to do is just have one layout and just copy it in each different folder or I also need to change values in each layout? 我需要做的就是只有一个布局,然后将其复制到每个不同的文件夹中,或者我还需要更改每个布局中的值?

You start by deleting the layout-sw120dp , layout-sw160dp , layout-sw240dp , layout-sw320dp , and layout-sw480dp directories that you created. 首先,删除您创建的layout-sw120dplayout-sw160dplayout-sw240dplayout-sw320dplayout-sw480dp目录。 Or, at least, ignore them for now. 或者,至少暂时不要理会它们。

You then start implementing your UI, putting layout resources in res/layout/ . 然后,您开始实现UI,将布局资源放在res/layout/

Then, for those layouts that need to be different in certain scenarios, you then create a directory representing the size when you want a different layout. 然后,对于在某些情况下需要不同布局的那些布局, 然后在需要不同布局时创建一个代表大小的目录。 Copy the layout from res/layout/ into the new directory, and then modify that copy to reflect whatever changes you want. 将布局从res/layout/复制到新目录,然后修改该副本以反映所需的任何更改。

In other words, one copy of every layout is in res/layout/ , and you override where needed with additional, modified copies in specific directories where you need the UI to change. 换句话说,每个布局的一个副本位于res/layout/ ,您需要在需要更改UI的特定目录中覆盖 其他修改的副本,以覆盖所需的位置。

This is an answer that's been an issue from old ages and for which you'll see lot many answers but which is not a one fit all type still. 这是一个很久以来就一直存在的问题,您会看到很多答案,但仍然不适合所有类型。 What I did come up with though when faced with the same issue was to use PercentRelativeLayout . 当遇到相同问题时,我的想法是使用PercentRelativeLayout This is a relatively new feature that was started from Android support version 23.0 (android sdk manager), and one of the big game changers according to me, since it allowed developers to specify their blocks relative to the layout size Percentage-wise. 这是一个相对较新的功能,它始于Android支持版本23.0 (android sdk管理器),并且对我来说是最大的改变者之一,因为它允许开发人员相对于布局大小按百分比指定开发人员。 And since it is in terms of percent, this fits all screen sizes with varying dimensions, but while maintaining the ratio . 并且由于以百分比表示,因此它适合所有具有不同尺寸的屏幕尺寸 ,但同时保持该比例

Ofcourse this method involves some trial and error and a lot of experimenting , but I found this method to be the easiest to implement and which took out the headache of maintaining the dimensions for various screen sizes. 当然,此方法涉及一些反复试验和大量实验 ,但是我发现该方法最容易实现,并且消除了为各种屏幕尺寸维护尺寸的麻烦。


Few tutorials to get you started : 很少有教程可以帮助您入门:

https://www.tutorialspoint.com/android/android_relative_layout.htm https://guides.codepath.com/android/Constructing-View-Layouts https://www.tutorialspoint.com/android/android_relative_layout.htm https://guides.codepath.com/android/Constructing-View-Layouts

If you want to use the same layout for each and every screen density, you don't need to create different folders. 如果要为每个屏幕密度使用相同的布局,则无需创建不同的文件夹。 Use just one simply called "layout" and the system will interpret it for every density. 仅使用一个简单的“布局”,系统就会针对每种密度对其进行解释。 However, this could lead to strange layouts on certain physical devices depending on their screen size and density... Another point you have to be aware of, if your application supports orientation changes, is that you have to design layouts for portrait and lanscape orientations. 但是,这可能会导致某些物理设备上的奇怪布局,具体取决于它们的屏幕大小和密度...如果您的应用程序支持方向更改,则您还必须意识到的另一点是,您必须设计纵向和横向的布局。 This is done by duplicating a folder used for a density and add "-port" or "-land" to inform the systen which one must be used according to the actual orientation of the device your app is currently running on. 通过复制用于密度的文件夹并添加“ -port”或“ -land”来通知系统,必须根据您的应用当前正在运行的设备的实际方向来使用哪个文件夹来完成此操作。 If you want to precisely define your app look and feel, you have to customize your layout for each density. 如果要精确定义应用程序的外观,则必须针对每种密度自定义布局。 And if you use bitmaps, you will have to customize them either (for example, your app icon should be defined with different sizes to keep a good looking for each screen density). 而且,如果您使用位图,则必须自定义它们(例如,应将应用程序图标定义为不同的大小,以保持每个屏幕密度的良好外观)。 Just as for the layout, you have to create "drawable-..." folders to contain the corresponding versions of your bitmaps. 就像布局一样,您必须创建“ drawable -...”文件夹以包含位图的相应版本。

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

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