宽度和高度
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); Point point = new Point(); display.getSize(point); SCREEN_WIDTH = point.x; SCREEN_HEIGHT = point.y; System.out.println("SCREEN_WIDTH = " + SCREEN_WIDTH); System.out.println("SCREEN_HEIGHT = " + SCREEN_HEIGHT);
状态栏高度
如果是做rom工作的同学可以使用如下方法:
int id = com.android.internal.R.dimen.status_bar_height; STATUS_BAR_HEIGHT = (int) getResources().getDimension(id);上面方法仅试用做rom的,因为在编译的时候,id的值已经赋值,比如com.android.internal.R.dimen.status_bar_height = 123;那么实际上面的代码就是这样
int id = 123; STATUS_BAR_HEIGHT = (int) getResources().getDimension(id);
还有一种就是上面方法的改进,利用Java反射原理,获取高度。
int id = 0; try { Class<?> cls = Class.forName("com.android.internal.R$dimen"); Field field = cls.getField("status_bar_height"); id = field.getInt(cls); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace); }