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

时间:2024-10-14 18:03:44

转自:http://blog.****.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;