Android获取状态栏高度

Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, statusBarHeight = 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());
    statusBarHeight = getResources().getDimensionPixelSize(x);
} catch(Exception e1) { }


这种方式可以在任何地方实现,而下面一段代码不能在onCreate方法里面获取到状态栏的高度(获取到的值是0)

Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;


你可能感兴趣的:(Android获取状态栏高度)