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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。