获取状态栏高度

Android中获取状态栏高度的两种方法:

     public static int getStatusHeight(Context context)
     {

        int statusHeight = 0;
        try
        {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int resourceId = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(resourceId);
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return statusHeight;
     }
     
     public static int getStatusHeight2(Context context) 
     {
         int statusHeight = 0;
         int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
         if (resourceId > 0) 
         {
             statusHeight = context.getResources().getDimensionPixelSize(resourceId);
         }
         return statusHeight;
    }

 

posted @ 2015-12-05 18:03  南极冰川雪  阅读(356)  评论(0编辑  收藏  举报