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

返回单位: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;

    }

你可能感兴趣的:(Android学习)