方法一:
publicintgetStatusBarHeight() {
intresult =0;
intresourceId = getResources().getIdentifier("status_bar_height","dimen","android");
if(resourceId >0) {
result = getResources().getDimensionPixelSize(resourceId);
}
returnresult;
}
方法二:
publicstaticintgetStatusHeight(Context context) {
intstatusHeight = -1;
try{
Class clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
intheight = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
}catch(Exception e) {
e.printStackTrace();
}
returnstatusHeight;
}
方法三:
不推荐使用,因为这个方法依赖于WMS(窗口管理服务的回调)。
Rect rectangle=newRect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
intstatusBarHeight= rectangle.top;