Andorid获取状态栏高度

方法一:

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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容