<span style="font-size:18px;">在Android中Window对象通常由PhoneWindow来实现,PhoneWindow将一个DecorView设置为整改应用窗口的根View</span>
<span style="font-size:18px;">final View decorView = getActivity().getWindow().getDecorView();</span>
<span style="font-size:18px;">设置了系统UI可见性改变监听器 decorView.setOnSystemUiVisibilityChangeListener( <span style="white-space:pre"> </span>new View.OnSystemUiVisibilityChangeListener() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onSystemUiVisibilityChange(int i) { <span style="white-space:pre"> </span>int height = decorView.getHeight(); Log.i(TAG, "Current height: " + height); } });</span>
<span style="font-size:18px;"></span><pre name="code" class="java"><span style="font-size:18px;">//</span><span style="font-family: Arial, Helvetica, sans-serif;">得到当前的UI标记</span>int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();int newUiOptions = uiOptions;
<span style="font-size:18px;"></span><pre name="code" class="java"><span style="font-size:18px;">//</span><span style="font-family: Arial, Helvetica, sans-serif;">判断是否为沉浸模式</span>boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);// Immersive mode: Backward compatible to KitKat (API 19).// Note that this flag doesn't do anything by itself, it only augments the behavior// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample// all three flags are being toggled together.// This sample uses the "sticky" form of immersive mode, which will let the user swipe// the bars back in again, but will automatically make them disappear a few seconds later.
<span style="font-size:18px;">newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN; newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;</span>
<span style="font-size:18px;">getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions); </span>
<span style="font-size:18px;"></span><pre name="code" class="java">//得到的容器包含状态栏和decor_content_parent ViewGroup group = (ViewGroup) getWindow().getDecorView();
<pre name="code" class="java"><span style="font-size:18px;">//顶部状态栏</span>View statubar = group.getChildAt(1);if (statubar != null) { int statuHeight = statubar.getMeasuredHeight(); Log.i(TAG, "statuHeight height: " + statuHeight);}
<pre name="code" class="java"><span style="font-size:18px;">//id为content的内容view</span>ViewGroup child = (ViewGroup) group.getChildAt(0);View contentView = child.getChildAt(0);if (contentView != null) { int contentHeight = contentView.getMeasuredHeight(); Log.i(TAG, "contentHeight height: " + contentHeight);}
<pre name="code" class="java"><span style="font-size:18px;">//actionbar</span>View titleView = child.getChildAt(1);if (titleView != null) { int titleHeight = titleView.getMeasuredHeight(); Log.i(TAG, "titleView height: " + titleHeight);}