开发中遇到了需要获得状态栏高度的问题,查阅了一下,在这做个笔记
状态栏高度的获取
//获得状态栏的高度
private int getStatusBarHeight(Context context) {
int ret = -1;
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());
ret = context.getResources().getDimensionPixelOffset(height);
} catch (Exception e){
e.printStackTrace();
}
return ret;
}
// 推荐的 获得状态栏高度的方式
private int getStatusBarHeight2(Context context){
int ret = -1;
int resourceId = getResources().getIdentifier("status_bar_height","dimen","android");
if (resourceId > 0){
ret = context.getResources().getDimensionPixelSize(resourceId);
}
return ret;
}