完美获取Android状态栏高度
16lz
2021-01-24
需要获取状态栏高度的问题。
就像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;
就像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;
更多相关文章
- Android中Intent的setData,setType和setDataAndType的用法
- android使用Glide加载RelativeLayout、LinearLayout等背景图片
- android 实现左右滑动效果
- Android(安卓)HAL 开发 (1)
- ExpandableListView 怎么和其他组件相互整合布局
- 时间轴控件TimeLineView的实现
- Google 手机操作系统 Android(安卓)开发教程 转载
- android上改变listView的选中颜色
- Android(安卓)动态替换 Manifest.xml中的值