简体   繁体   English

如何在Android 2.3.3 SDK中以编程方式确定屏幕方向和大小桶

[英]How to determine screen orientation and size bucket programatically in Android 2.3.3 SDK

My application is supposed decide whether to display in dual-pane or single-pane using the following logic: 我的应用程序应该使用以下逻辑决定是在双窗格还是单窗格中显示:

  1. For ALL screen sizes, if in portrait display in SINGLE-PANE 对于所有屏幕尺寸,如果在SINGLE-PANE中进行纵向显示
  2. For SMALL and NORMAL screen sizes, if in landscape display in SINGLE-PANE 对于SMALL和NORMAL屏幕尺寸,如果在SINGLE-PANE中以横向显示
  3. For LARGE and XLARGE screen sizes, if in landscape display in DUAL-PANE 对于LARGE和XLARGE屏幕尺寸,如果在DUAL-PANE中以横向显示

I am developing for Android 2.3.3 SDK, so the nice features of Android 3.0 SDK are not applicable here. 我正在开发Android 2.3.3 SDK,所以Android 3.0 SDK的不错功能在这里不适用。 The function I have developed below always returns SINGLE-PANE for all screen sizes and orientation combinations. 我在下面开发的功能总是为所有屏幕尺寸和方向组合返回SINGLE-PANE。 What could be going wrong here? 这可能会出错?

/**
 * Determine if a dual pane is appropriate.
 * 
 * @param null
 * @return boolean
 */

public boolean isDualPane() {

    boolean dualPane    = false;

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

        if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {

            dualPane    = true;
        }
    }

    Log.d("ORIANTATION", (dualPane) ? "DUAL PANE" : "SINGLE PANE");

    return dualPane;
}

I have found the answer. 我找到了答案。 There is nothing wrong with the above function! 上面的功能没有错! The problem was with the configuration of my emulator. 问题在于我的模拟器的配置。 I created a WVGA800 device and left the LCD density at 240 instead of 160. That makes it a normal size and not a large size. 我创建了一个WVGA800设备,并将LCD密度保持在240而不是160.这使它成为正常尺寸而不是大尺寸。 So the statememnt Configuration.SCREENLAYOUT_SIZE_LARGE always evaluated to 2 instead of 3. So the if statement failed each time. 因此,statememnt Configuration.SCREENLAYOUT_SIZE_LARGE始终计算为2而不是3.因此if statement每次都失败。 It works fine now. 它现在工作正常。

One more thing, this : 还有一点, 这个

        if ((getResources().getConfiguration().screenLayout 
           & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {

            dualPane    = true;
        }

needs to be changed to: 需要改为:

        if ((getResources().getConfiguration().screenLayout 
            & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {

            dualPane    = true;
        }

In order to cater for XLARGE screens as well. 为了满足XLARGE屏幕的需求。

I dont know much about dual pane, but I suggest you control the configuration Small-Normal-Large-Xlarge layout from XML. 我不太了解双窗格,但我建议您从XML控制配置Small-Normal-Large-Xlarge布局。

Example : 示例:

  1. All screen size - portrait. 所有屏幕尺寸 - 肖像。 it takes the default layout folder : res/layout 它采用默认布局文件夹: res / layout
  2. Small screen size - landscape : res/layout-small-land 小屏幕尺寸 - 风景: res / layout-small-land
  3. Large Xlarge screen size - landscape : res/layout-large-land 大型Xlarge屏幕尺寸 - 风景: res / layout-large-land

It maybe Helping you. 它可能会帮助你。 :) :)

Refference : Screen size 参考: 屏幕尺寸

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

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