[英]How can I get android Honeycomb system's screen width and height?
my code: 我的代码:
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);
when I run this program in android Honeycomb system,the output is 800 and 1232 but the device's screen is 800_1280,hao can I get it? 当我在android Honeycomb系统中运行这个程序时,输出是800和1232,但设备的屏幕是800_1280,我可以得到它吗? thanks.
谢谢。
This piece of code works for me (if you don't mind to use undocumented methods): 这段代码适合我(如果你不介意使用未记录的方法):
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 x 752 - landscape 800 x 1232 - portrait 1280 x 752 - 风景800 x 1232 - 肖像
This 48 px are for the Andriod default notification bar... 这个48像素用于Andriod默认通知栏...
Actually device full screen width=800 and height=1280
(including status bar, title bar)
. 设备全屏
width=800 and height=1280
(including status bar, title bar)
。 If you run your code to get display size, the system will not include status bar height and title bar height, so you will get height as 1232 (1280-(status bar height + title bar height))
. 如果运行代码以获得显示大小,系统将不包括状态栏高度和标题栏高度,因此您将获得
height as 1232 (1280-(status bar height + title bar height))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.