有的时候我们开发项目需要获取安卓状态栏高度,那么可以使用下面这个方法获取!
/*获取通知栏高度*/
private int getStatusBarHeight(){
int startBarHeight = 50;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
startBarHeight = getResources().getDimensionPixelSize(height);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (java.lang.InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
Log.w("您的通知栏高度:", String.valueOf(startBarHeight));
return startBarHeight;
}