Android开发之获取状态栏高度、屏幕的宽和高
转自:http://blog.csdn.net/guolin_blog/article/details/16919859
获取状态栏的高度。
1 private static int statusBarHeight; 2 3 private int getStatusBarHeight() { 4 if (statusBarHeight == 0) { 5 try { 6 Class<?> c = Class.forName("com.android.internal.R$dimen"); 7 Object o = c.newInstance(); 8 Field field = c.getField("status_bar_height"); 9 int x = (Integer) field.get(o); 10 statusBarHeight = getResources().getDimensionPixelSize(x); 11 } catch (Exception e) { 12 e.printStackTrace(); 13 } 14 } 15 return statusBarHeight; 16 }
--------------------------------------------------------------------
获取屏幕的宽和高
方法一:
1 WindowManager mWm = (WindowManager) getSystemService(WINDOW_SERVICE); 2 int screenWidth = mWm.getDefaultDisplay().getWidth(); 3 int screenHeight = mWm.getDefaultDisplay().getHeight();
注意:getWidth()和getHeight()方法已经过时
方法二:
1 WindowManager mWm = (WindowManager) getSystemService(WINDOW_SERVICE); 2 Point point= new Point(); 3 mWm.getDefaultDisplay().getRealSize(point); 4 int screenWidth=point.x; 5 int screenHeight=point.y;
直面挑战,躬身入局