梦书之家(移动开发)

你有一个苹果,我有一个苹果,我们交换一下,一人还是一个苹果;你有一个思想,我有一个思想,我们交换一下,一人就有两个思想。 ——肖伯纳

导航

Android下获取状态栏的高度

    /**
     * 
     * @param activity
     * @return > 0 success; <= 0 fail
     */
    public static 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; }

 用以上代码测试了下小米手机1(480X854)的状态栏的高度是:38

posted on 2012-12-06 10:48  梦书  阅读(8363)  评论(0编辑  收藏  举报