获取通知栏高度

来源:互联网 发布:python教程书籍推荐 编辑:程序博客网 时间:2024/06/05 18:25

网上有很多种,很多是需要getWindow()方法,必须在Activity里;但是最近我要在fragment中得到通知栏高度,而且最好是提出公用方法可以在任何地方调用获取。

所以查找资料整理了几个:


公用方法的:

    public static int getStatusBarHeight(Context context){              Class<?> c = null;              Object obj = null;              Field field = null;              int x = 0, statusBarHeight = 0;              try {                  c = Class.forName("com.android.internal.R$dimen");                  obj = c.newInstance();                  field = c.getField("status_bar_height");                  x = Integer.parseInt(field.get(obj).toString());                  statusBarHeight = context.getResources().getDimensionPixelSize(x);              } catch (Exception e1) {                  e1.printStackTrace();              }               return statusBarHeight;          }  

经常用的,最常见的:

    private int getStatusBarHeight(){         Rect rect = new Rect();         getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);         return rect.top;      }  

在别人整理的看见过,但没用过

    public static int getStatusBarHeight() {              return Resources.getSystem().getDimensionPixelSize(                      Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));          }  


0 0
原创粉丝点击