需要获取状态栏高度的问题。

就像android后期版本,无法直接退出一样。找了一些方法来获取状态栏高度,结果都是为0.

还好,牛人是很多的,当时,找到一段代码,能够有效的获取状态栏的高度。特此记录,备忘,以及供大家参考。

Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, sbar = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
sbar = getResources().getDimensionPixelSize(x);
} catch(Exception e1) {
loge("get status bar height fail");
e1.printStackTrace();
}
个人注:以下代码不能在onCreate里面使用,否则获取状态栏高度为0

Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

int statusBarHeight = frame.top;

更多相关文章

  1. Android中Intent的setData,setType和setDataAndType的用法
  2. android使用Glide加载RelativeLayout、LinearLayout等背景图片
  3. android 实现左右滑动效果
  4. Android(安卓)HAL 开发 (1)
  5. ExpandableListView 怎么和其他组件相互整合布局
  6. 时间轴控件TimeLineView的实现
  7. Google 手机操作系统 Android(安卓)开发教程 转载
  8. android上改变listView的选中颜色
  9. Android(安卓)动态替换 Manifest.xml中的值

随机推荐

  1. c语言函数声明格式
  2. c语言中定义结构体的保留字是什么?
  3. visualc++6.0怎么使用
  4. c语言中continue作用是什么?
  5. 手把手教你在C语言中如何打印倒三角形
  6. strncmp函数用法详解
  7. C语言中有且唯一的函数是什么函数
  8. c语言合法标识符判断规则是什么?
  9. c语言数组在内存中是怎么分配的?
  10. c语言return返回值给谁