Android获取状态栏的高度

项目中用到了,网上找到一段代码,能够有效的获取状态栏的高度,记录备忘。

[java]  view plain copy
  1. /** 
  2.      * 获取状态栏高度 
  3.      *  
  4.      * @return 
  5.      */  
  6.     public int getStatusBarHeight()  
  7.     {  
  8.         Class<?> c = null;  
  9.         Object obj = null;  
  10.         java.lang.reflect.Field field = null;  
  11.         int x = 0;  
  12.         int statusBarHeight = 0;  
  13.         try  
  14.         {  
  15.             c = Class.forName("com.android.internal.R$dimen");  
  16.             obj = c.newInstance();  
  17.             field = c.getField("status_bar_height");  
  18.             x = Integer.parseInt(field.get(obj).toString());  
  19.             statusBarHeight = getResources().getDimensionPixelSize(x);  
  20.             return statusBarHeight;  
  21.         }  
  22.         catch (Exception e)  
  23.         {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return statusBarHeight;  
  27.     }  
  28. 方法二:获取状态栏高度和标题栏高度:
  29. private void getBarHeight()
    {
    // 获取状态栏高度
    Rect frame = new Rect();
    this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    statusBarHeight = frame.top;

    int contenttop = this.getWindow()
    .findViewById(Window.ID_ANDROID_CONTENT).getTop();
    // statusBarHeight是上面所求的状态栏的高度
    titleBarHeight = contenttop - statusBarHeight;

    Log.v(TAG, "statusBarHeight = " + statusBarHeight
    + ", titleBarHeight = " + titleBarHeight);
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值