您的位置:首页 > 移动开发 > Android开发

android 获取状态栏高度为0的解决办法

2015-12-18 15:50 537 查看
/**
* 获取通知栏的高度
* @param activity
* @return
*/
public static int getStatusBarHeight(Activity activity){
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
return rect.top;
}
/**
* 获取通知栏(状态栏)的高度
*
* @return statusBarHeight
*/
private int getStatusBarHeight() {
Class<?> clazz;
Object object;
Field field;
int x;
int statusBarHeight = 0;

try {
clazz = Class.forName("com.android.internal.R$dimen");
object = clazz.newInstance();
field = clazz.getField("status_bar_height");
x = Integer.parseInt(field.get(object).toString());
statusBarHeight = getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息