方法一:
/**
* 用于获取状态栏的高度。
*
* @return 返回状态栏高度的像素值。
*/
private
int
getStatusBarHeight() {
int
statusBarHeight =
0
;
try
{
Class<?> c = Class.forName(
"com.android.internal.R$dimen"
);
Object o = c.newInstance();
Field field = c.getField(
"status_bar_height"
);
int
x = (Integer) field.get(o);
statusBarHeight = getResources().getDimensionPixelSize(x);
}
catch
(Exception e) {
e.printStackTrace();
Rect frame =
new
Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
statusBarHeight = frame.top;
}
return
statusBarHeight;
}
方法二:(推荐)
private
int
getStatusBarHeight() {
int
result =
0
;
int
resourceId = getResources().getIdentifier(
"status_bar_height"
,
"dimen"
,
"android"
);
if
(resourceId >
0
) {
result = res.getDimensionPixelSize(resourceId);
}
return
result;
}