-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathWindowUtil.java
36 lines (32 loc) · 1.15 KB
/
WindowUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example.jiangrui.myapplication;
import android.app.Activity;
import android.graphics.Rect;
import java.lang.reflect.Field;
/**
* Android Window相关工具类
*/
public class WindowUtil {
// 获取设备状态栏高度,px值
// 使用了两种方式。如果方式一不起作用,则采用方式二(反射)
public int getStatusBarHeight(Activity myActivityReference) {
Rect frame = new Rect();
myActivityReference.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
if (statusBarHeight == 0) {
Class<?> c;
Object obj;
Field field;
int x;
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());
statusBarHeight = myActivityReference.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
}
return statusBarHeight;
}
}