首页 > 代码库 > Android下获取状态栏的高度

Android下获取状态栏的高度

/**
<!-- lang: java -->
 * 
<!-- lang: java -->
 * @param activity
<!-- lang: java -->
 * @return > 0 success; <= 0 fail
<!-- lang: java -->
 */
<!-- lang: java -->
 public static int getStatusHeight(Activity activity){
<!-- lang: java -->
 int statusHeight = 0;
<!-- lang: java -->
 Rect localRect = new Rect();
<!-- lang: java -->
 activity.getWindow().getDecorView(
<!-- lang: java -->
        ).getWindowVisibleDisplayFrame(localRect);
<!-- lang: java -->
 statusHeight = localRect.top;
<!-- lang: java -->
 if (0 == statusHeight){
<!-- lang: java -->
 Class<?> localClass;
<!-- lang: java -->
 try {
<!-- lang: java -->
 localClass = Class.forName(
<!-- lang: java -->
            "com.android.internal.R$dimen");
<!-- lang: java -->
 Object localObject = localClass.newInstance();
<!-- lang: java -->
 int i5 = Integer.parseInt(
<!-- lang: java -->
              localClass.getField("status_bar_height").get(
<!-- lang: java -->
                localObject).toString());
<!-- lang: java -->
 statusHeight = activity.getResources(
<!-- lang: java -->
            ).getDimensionPixelSize(i5);
<!-- lang: java -->
 } catch (ClassNotFoundException e) {
<!-- lang: java -->
 e.printStackTrace();
<!-- lang: java -->
 } catch (IllegalAccessException e) {
<!-- lang: java -->
 e.printStackTrace();
<!-- lang: java -->
 } catch (InstantiationException e) {
<!-- lang: java -->
 e.printStackTrace();
<!-- lang: java -->
 } catch (NumberFormatException e) {
<!-- lang: java -->
 e.printStackTrace();
<!-- lang: java -->
 } catch (IllegalArgumentException e) {
<!-- lang: java -->
 e.printStackTrace();
<!-- lang: java -->
 } catch (SecurityException e) {
<!-- lang: java -->
 e.printStackTrace();
<!-- lang: java -->
 } catch (NoSuchFieldException e) {
<!-- lang: java -->
 e.printStackTrace();
<!-- lang: java -->
 }
<!-- lang: java -->
 }
<!-- lang: java -->
 return statusHeight;
<!-- lang: java -->
 }

原文地址 http://bbs.it-home.org/thread-16887-1-1.html

Android下获取状态栏的高度