android获取状态栏与标题栏的高度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
int top1,top2;
Rectframe=new Rect();
this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
top1=frame.top;
int contentTop=this.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
top2=contentTop-top1;
Stringtext="状态栏的高度为:"+top1+"n"+"标题栏的高度为:"+top2;
this.tv1.setText(text);
}
decorView是Window中最顶层的View,可以从window中获取到 decorView,然后decorView有个:
getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏但不包括状态栏。
所以状态栏的高度就为:
Rect frame=new Rect();
this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
top1=frame.top;
getWindow().findViewById(Window.ID_ANDROID_CONTENT)得到的view就是程序不包括状态栏与 标题栏的部分。
注意: 在onCreate中不能得到这些值,要在onWindowFocusChanged中才能得到。