我怎样才能获得android Honeycomb系统的屏幕宽度和高度?

时间:2011-08-22 08:29:59

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

我的代码:

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,我可以得到它吗? 感谢。

3 个答案:

答案 0 :(得分:6)

这段代码适合我(如果你不介意使用未记录的方法):

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);

答案 1 :(得分:0)

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

答案 2 :(得分:0)

1280 x 752 - 景观 800 x 1232 - 肖像

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