android 通过反射方法获取状态栏高度

RT,直接贴代码

 1     /**
 2      * get the status_bar_height
 3      * @param activity
 4      * @return
 5      */
 6     public static int getStatusHeight(Activity activity) {
 7         int statusHeight = 0;
 8         Class<?> localClass = null;
 9         try {
10             localClass = Class.forName("com.android.internal.R$dimen");
11             Object localObject = localClass.newInstance();
12             int h = Integer.parseInt(localClass.getField("status_bar_height")
13                     .get(localObject).toString());
14             statusHeight = activity.getResources().getDimensionPixelSize(h);
15         } catch (Exception e) {
16             e.printStackTrace();
17         }
18         return statusHeight;
19     }

 

posted @ 2014-01-23 16:48  wesley.du  阅读(443)  评论(0编辑  收藏  举报