系统状态栏 & 程序TiTleBar 高度的获取
只有在activity的onWindowFocusChanged方法中才能获取到系统状态栏以及TiTleBar的高度
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); System.out.println("-+--+--+-onWindowFocusChanged-+--+--+-"); Rect rect = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int top = rect.top; System.out.println("top:"+top); View view = getWindow().findViewById(Window.ID_ANDROID_CONTENT); int top2 = view.getTop(); System.out.println("top2:"+top2); int height = getWindowManager().getDefaultDisplay().getHeight(); System.out.println("height:"+height); }
更改清单文件<application>节点下的theme属性后,参考日志信息,很容易得出以上top以及top2的含义
android:theme="@android:style/Theme.Light"
07-11 22:56:00.483: I/System.out(7934): -+--+--+-onWindowFocusChanged-+--+--+-
07-11 22:56:00.483: I/System.out(7934): top:75
07-11 22:56:00.483: I/System.out(7934): top2:150
07-11 22:56:00.483: I/System.out(7934): height:1776
android:theme="@android:style/Theme.Light.NoTitleBar"
07-11 23:35:14.615: I/System.out(11297): -+--+--+-onWindowFocusChanged-+--+--+-
07-11 23:35:14.615: I/System.out(11297): top:75
07-11 23:35:14.615: I/System.out(11297): top2:75
07-11 23:35:14.615: I/System.out(11297): height:1776
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
07-11 23:41:02.276: I/System.out(13137): -+--+--+-onWindowFocusChanged-+--+--+-
07-11 23:41:02.276: I/System.out(13137): top:75
07-11 23:41:02.276: I/System.out(13137): top2:0
07-11 23:41:02.276: I/System.out(13137): height:1776
结论:
①获取系统系统状态栏高度(也就是系统最上方显示时间信号以及notification通知的部分)
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statebarHeight= rect.top;
②获取系统状态栏以及程序TiTleBar高度和
View view = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
int viewHeight = view.getTop();
③以上②减去①就是程序状态栏的高度

浙公网安备 33010602011771号