Android开发之获取状态栏高度、屏幕的宽和高

转自:http://blog.csdn.net/guolin_blog/article/details/16919859

获取状态栏的高度。

     private static int statusBarHeight;

     private int getStatusBarHeight() {
if (statusBarHeight == 0) {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object o = c.newInstance();
Field field = c.getField("status_bar_height");
int x = (Integer) field.get(o);
statusBarHeight = getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
}
return statusBarHeight;
}

--------------------------------------------------------------------

获取屏幕的宽和高

方法一:

         WindowManager mWm = (WindowManager) getSystemService(WINDOW_SERVICE);
int screenWidth = mWm.getDefaultDisplay().getWidth();
int screenHeight = mWm.getDefaultDisplay().getHeight();

注意:getWidth()和getHeight()方法已经过时

方法二:

         WindowManager mWm = (WindowManager) getSystemService(WINDOW_SERVICE);
Point point= new Point();
mWm.getDefaultDisplay().getRealSize(point);
int screenWidth=point.x;
int screenHeight=point.y;
上一篇:正则表达式中的\n


下一篇:自动生成 java 测试 mock 对象框架 DataFactory-01-入门使用教程