Android下获取状态栏的高度

来源:互联网 发布:tomcat源码分析 编辑:程序博客网 时间:2024/04/30 07:03

Android平台中获取状态栏的高度代码实现

    /**     *      * @param activity     * @return > 0 success; <= 0 fail     */    public int getStatusHeight(Activity activity){        int statusHeight = 0;        Rect localRect = new Rect();        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);        statusHeight = localRect.top;        if (0 == statusHeight){            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 = activity.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;    }
在HTC G11,系统为官方4.04版本的手机上进行测试过,高度为 38。


原创粉丝点击