如何获得android蜂窝系统的屏幕宽度和高度?
问题描述:
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,郝我能怎么做呢? 谢谢。
答
其实设备全屏width=800 and height=1280
(including status bar, title bar)
。如果您运行代码以获取显示大小,系统将不包含状态栏高度和标题栏高度,因此您将获得height as 1232 (1280-(status bar height + title bar height))
。
答
这段代码对我的作品(如果你不介意使用无证方法):
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);
答
1280 * 752 - 景观 为800 x 1232 - 肖像
这48像素的为Andriod默认通知栏...
看起来它只是告诉你窗口大小 - 如果你将它设置为全屏幕,你可能会得到完整的大小 – cjk
你如何获得这些尺寸值? 1232的高度实际上是屏幕尺寸减去导航栏高度(底部带有后退,首页等按钮的栏)(即1280-48 = 1232)。所以你正在为你的活动获得可用的屏幕空间(假设你将它设置为无标题栏)。事情是我已经尝试了相同的代码,但我获得了800x1280的绝对屏幕尺寸。我实际上想知道**你是如何返回1232的。我在3.1版上测试了我的代码 –