Chinaunix首页 | 论坛 | 博客
  • 博客访问: 420065
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-04-25 18:08:10


StatusBarHeightUtil.java

  1. package com.vibexie.jianai.Utils;

  2. import android.content.Context;
  3. import android.view.View;

  4. /**
  5.  * 利用反射机制获取状态栏高度的工具类
  6.  * Created by vibexie on 4/25/15.
  7.  */
  8. public class StatusBarHeightUtil {
  9.     /**
  10.      * 在activity中调用
  11.      * @param context
  12.      * @return
  13.      */
  14.     public static int getStatusBarHeight(Context context){
  15.         Class<?> c = null;
  16.         Object obj = null;
  17.         java.lang.reflect.Field field = null;
  18.         int x = 0;
  19.         int statusBarHeight = 0;
  20.         try{
  21.             c = Class.forName("com.android.internal.R$dimen");
  22.             obj = c.newInstance();
  23.             field = c.getField("status_bar_height");
  24.             x = Integer.parseInt(field.get(obj).toString());
  25.             statusBarHeight = context.getResources().getDimensionPixelSize(x);
  26.             return statusBarHeight;
  27.         }catch (Exception e){
  28.             e.printStackTrace();
  29.         }

  30.         return statusBarHeight;
  31.     }

  32.     /**
  33.      * 在自定义View中调用
  34.      * @param view
  35.      * @return
  36.      */
  37.     public static int getStatusBarHeight(View view){
  38.         Class<?> c = null;
  39.         Object obj = null;
  40.         java.lang.reflect.Field field = null;
  41.         int x = 0;
  42.         int statusBarHeight = 0;
  43.         try{
  44.             c = Class.forName("com.android.internal.R$dimen");
  45.             obj = c.newInstance();
  46.             field = c.getField("status_bar_height");
  47.             x = Integer.parseInt(field.get(obj).toString());
  48.             statusBarHeight = view.getResources().getDimensionPixelSize(x);
  49.             return statusBarHeight;
  50.         }catch (Exception e){
  51.             e.printStackTrace();
  52.         }

  53.         return statusBarHeight;
  54.     }
  55. }

阅读(2420) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~