安卓开发 获取手机状态栏的高度

来源:互联网 发布:淘宝怎么发布话费充值 编辑:程序博客网 时间:2024/06/01 15:41

返回单位:px

方法一 :

    public static int getStatusHeight(Context context) {            int statusHeight = 0;            Class<?> localClass;            try {                localClass = Class.forName("com.android.internal.R$dimen");                Object localObject = localClass.newInstance();                int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());                statusHeight = context.getResources().getDimensionPixelSize(i5);            } catch (ClassNotFoundException e) {                e.printStackTrace();            } catch (IllegalAccessException e) {                e.printStackTrace();            } catch (InstantiationException e) {                e.printStackTrace();            } catch (NumberFormatException e) {                e.printStackTrace();            } catch (IllegalArgumentException e) {                e.printStackTrace();            } catch (SecurityException e) {                e.printStackTrace();            } catch (NoSuchFieldException e) {                e.printStackTrace();            }        return statusHeight;    }

方法二 :

    public int getStatusBarHeight(Context mContext) {        int height = 0;        int resId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");        if (resId > 0) {            height = mContext.getResources().getDimensionPixelSize(resId);        }        return height;    }
1 0