简体   繁体   English

Android上不同屏幕尺寸的不同布局?

[英]Different Layouts For Different Screen Sizes On Android?

So I made an app using in Eclipse using the Graphical Editor, AbsoluteLayout , fixed pixel values, etc... just bad practice in general. 所以我在Eclipse中使用图形编辑器, AbsoluteLayout ,固定像素值等制作了一个应用程序......一般来说只是不好的做法。 It defaulted to a 3.7in screen . 它默认为3.7in screen Is there any way to design a separate layout for each screen size and have the program choose which to load based on said screen size? 有没有办法为每个屏幕尺寸design separate layout ,并让程序根据所述屏幕尺寸选择加载哪个?

Provide different layouts for different screen sizes By default, Android resizes your application layout to fit the current device screen. 为不同的屏幕大小提供不同的布局默认情况下,Android会调整应用程序布局的大小以适应当前的设备屏幕。 In most cases, this works fine. 在大多数情况下,这很好。 In other cases, your UI might not look as good and might need adjustments for different screen sizes. 在其他情况下,您的UI可能看起来不太好,可能需要针对不同的屏幕尺寸进行调整。 For example, on a larger screen, you might want to adjust the position and size of some elements to take advantage of the additional screen space, or on a smaller screen, you might need to adjust sizes so that everything can fit on the screen. 例如,在较大的屏幕上,您可能需要调整某些元素的位置和大小以利用额外的屏幕空间,或者在较小的屏幕上,您可能需要调整大小以使所有内容都适合屏幕。
The configuration qualifiers you can use to provide size-specific resources are small, normal, large, and xlarge. 可用于提供特定于大小的资源的配置限定符是small,normal,large和xlarge。 For example, layouts for an extra large screen should go in layout-xlarge/. 例如,超大屏幕的布局应该在layout-xlarge /中。

Beginning with Android 3.2 (API level 13), the above size groups are deprecated and you should instead use the sw<N>dp configuration qualifier to define the smallest available width required by your layout resources. 从Android 3.2(API级别13)开始,不推荐使用上述大小组,您应该使用sw <N> dp配置限定符来定义布局资源所需的最小可用宽度。 For example, if your multi-pane tablet layout requires at least 600dp of screen width, you should place it in layout-sw600dp/ . 例如,如果多窗格平板电脑布局需要至少600dp的屏幕宽度,则应将其放在layout-sw600dp /中 Using the new techniques for declaring layout resources is discussed further in the section about Declaring Tablet Layouts for Android 3.2. 使用新技术声明布局资源将在有关为Android 3.2声明平板电脑布局的部分中进一步讨论。

From the Android developer site - just use layout-small/ layout-xlarge/ etc.. folder :=) best way ever.. 从Android 开发者网站 - 只需使用layout-small / layout-xlarge / etc ..文件夹:=)最好的方式..

I fixed this by running this code at startup 我通过在启动时运行此代码来修复此问题

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

From there I do an if statement to check for resolution and load a specific layout made for that resolution. 从那里我做一个if语句来检查分辨率并加载为该分辨率制作的特定布局。 EX. EX。 WVGA screen is 800x480 so I check for that and load the layout. WVGA屏幕是800x480所以我检查并加载布局。

if (width == 480 && height == 800)
    {
        setContentView(R.layout.main);
    }

This was for API < 13, now those functions are deprecated, see this Get screen dimensions in pixels 这是针对API <13,现在这些函数已被弃用,请参阅此屏幕尺寸以像素为单位

Instead just make different folders in the "res" folder, "layout-small" for small screens, "layout-large" for large screens, "layout-xlarge" for extralarge screens. 相反,只需在“res”文件夹中设置不同的文件夹,为小屏幕设置“layout-small”,为大屏幕设置“layout-large”,为超大屏幕设置“layout-xlarge”。 Design the layouts accordingly.click here for more info.. 相应地设计布局。 点击此处了解更多信息..

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

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