Android获取状态栏高度

2022-04-21  本文已影响0人  达文西_阿泽

状态栏

状态栏样式

方式一(推荐

        //获取状态栏的高度
  private int getStatusBarHeight(Activity activity) {
      int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
      if (resourceId > 0) {
          return activity.getResources().getDimensionPixelSize(resourceId);
      }
      return 0;
  }

方式二

    private int getStatusBarHeight() {
        int statusBarHeight = 0;
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object object = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = (Integer) field.get(object);
            statusBarHeight = getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusBarHeight;
    }
上一篇下一篇

猜你喜欢

热点阅读