【问题标题】:How can I get android Honeycomb system's screen width and height?如何获取 android Honeycomb 系统的屏幕宽度和高度?
【发布时间】:2011-10-31 23:15:59
【问题描述】:

我的代码:

Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);

System.out.println("screen width:"+displayMetrics.widthPixels);
System.out.println("screen heigth:"+displayMetrics.heightPixels);

当我在 android Honeycomb 系统中运行这个程序时,输出是 800 和 1232 但是设备的屏幕是800_1280,我可以得到吗? 谢谢。

【问题讨论】:

  • 看起来它只是告诉你窗口大小 - 如果你将它设置为全屏,你可能会得到完整大小
  • 您如何获得这些维度值? 1232 的高度实际上是屏幕尺寸减去导航栏高度(底部带有返回、主页等按钮的栏)(即 1280 - 48 = 1232)。因此,您将获得 Activity 的可用屏幕空间(假设您将其设置为无标题栏)。问题是我尝试了与您相同的代码,但我得到了 800x1280 的绝对屏幕尺寸返回给我。我实际上想知道 YOU 如何让它返回 1232。我在 3.1 版上测试了我的代码

标签: android height width screen android-3.0-honeycomb


【解决方案1】:

这段代码对我有用(如果您不介意使用未记录的方法):

Display d = getWindowManager().getDefaultDisplay();
int width, height;

Method mGetRawH;
Method mGetRawW;
try {
    mGetRawH = Display.class.getMethod("getRawWidth");
    mGetRawW = Display.class.getMethod("getRawHeight");
    width = (Integer) mGetRawW.invoke(d);
    height = (Integer) mGetRawH.invoke(d);
} catch (NoSuchMethodException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}

//You should care about orientation here
int o = getResources().getConfiguration().orientation;
if (o == Configuration.ORIENTATION_PORTRAIT) {
    if (width > height) {
        int tmp = width;
        width = height;
        height = tmp;
    }
} else if (o == Configuration.ORIENTATION_LANDSCAPE) {
    if (width < height) {
        int tmp = width;
        width = height;
        height = tmp;
    }
}

Log.d("d", "w=" + width + " h=" + height);

【讨论】:

    【解决方案2】:

    其实设备全屏width=800 and height=1280(including status bar, title bar)。如果你运行你的代码来获取显示大小,系统不会包含状态栏高度和标题栏高度,所以你会得到height as 1232 (1280-(status bar height + title bar height))

    【讨论】:

      【解决方案3】:

      1280 x 752 - 横向 800 x 1232 - 纵向

      这 48 px 用于 Andriod 默认通知栏...

      【讨论】:

        猜你喜欢
        • 2011-09-25
        • 1970-01-01
        • 2011-06-12
        • 2010-11-25
        • 1970-01-01
        • 2011-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多