码迷,mamicode.com
首页 > 移动开发 > 详细

Android下获取状态栏的高度

时间:2014-09-27 18:04:30      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:android   http   io   os   ar   java   for   sp   on   

/**
<!-- 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下获取状态栏的高度

标签:android   http   io   os   ar   java   for   sp   on   

原文地址:http://my.oschina.net/xiaobei668/blog/322622

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!