Android 用反射方式获取状态条高度

private static int getStatusBarHeight(Context context) {
        Class<?> c;
        Object obj;
        Field field;
        int x;
        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());
            return context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            Log.e("get status bar height fail", "");
            e1.printStackTrace();
            return -1;
        }
    }

posted @ 2013-06-03 16:19  云海天际  阅读(229)  评论(0编辑  收藏  举报