沉浸式显示效果,这是在 Android 4.4 (API Level 19) 引入的一个概念,平常开发中常常能接触到,官方介绍 Using Immersive Full-Screen Mode 。html
Android 的 System bars 包含 Status bar 和 Navigation bar,以下图所示顶部状态栏是 Status bar,底部虚拟按键栏是 Navigation bar。java
一般 System Bars 会和你的应用布局同时显示在屏幕上。应用为了能够沉浸式显示内容,能够经过暂时淡化 System bars 来实现减小分散用户注意力的体验,或者经过暂时隐藏 System bars 来实现彻底沉浸式的的体验。具体可参考这篇文章 管理 System window barsandroid
在平常开发中,咱们主要是处理 Activity 的沉浸效果,在全屏沉浸模式中弹出 Dialog 或 PopupWindow,则会自动退出沉浸模式。退出沉浸式的缘由是由于 Activity 的 Window 焦点被抢走了,Window 中的 DecorView 状态改变致使了退出。ide
Android 中的 Window 表示一个窗口的概念,Android 中全部的视图都是经过 Window 来呈现的,不管是 Activity、Dialog 或是 PopupWindow、Toast ,他们的视图实际上都是附加在 Window 上的。工具
Window 共有三种类型, 分别是应用 Window、子 Window、系统 Window。其中应用类 Window 对应着一个 Activity,子 Window 不能单独存在,他须要附属在特定的父 Window 中。好比常见的 Dialog 和 PopupWindow 就是一个子 Window。系统 Window 是须要声明权限才能建立的 Window,好比 Toast 和一些系统悬浮窗口都是系统的 Window。布局
咱们能够经过 Android Studio 的 Layout Inspector 工具查看一个简单 Activity 的 View Tree 结构。ui
能够看到不管是系统状态栏或是虚拟按键栏都是附在 DecorView 上,要避免 DecorView 状态改变而致使沉浸式模式退出。除了设置在 Activity 的 onWindowFocusChanged 方法状态时从新设置进入沉浸式模式,还须要把 Dialog 和 PopupWindow 弹出时形成的焦点改变也考虑进去。this
下面给出在 Activity 、Dialog 和 PopupWindow 中的沉浸式效果的具体实现。spa
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.xxx);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
fullScreenImmersive(mDecorView);
}
复制代码
/** * 全屏显示,隐藏虚拟按钮 * @param view */
private void fullScreenImmersive(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
view.setSystemUiVisibility(uiOptions);
}
}
复制代码
Dialog 在初始化时会生成新的 Window,先禁止 Dialog Window 获取焦点,等 Dialog 显示后对 Dialog Window 的 DecorView 设置 setSystemUiVisibility ,接着再获取焦点,这样看起来就没有退出沉浸模式。code
public class LoadingDialog extends Dialog {
@Override
public void show() {
// Dialog 在初始化时会生成新的 Window,先禁止 Dialog Window 获取焦点,等 Dialog 显示后对 Dialog Window 的 DecorView 设置 setSystemUiVisibility ,接着再获取焦点。 这样表面上看起来就没有退出沉浸模式。
// Set the dialog to not focusable (makes navigation ignore us adding the window)
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
//Show the dialog!
super.show();
//Set the dialog to immersive
fullScreenImmersive(getWindow().getDecorView());
//Clear the not focusable flag from the window
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
}
复制代码
而 PopupWindow 并无建立新的 Window,只是将 PopupWindow 的 View 添加到当前的 WindowManager。不过能够对 PopupWindow 设置 setFocusable。
同理,先在失焦的状态,弹出 PopupWindow , 再对 PopupWindow 的 DecorView 设置 setSystemUiVisibility ,最后获取焦点便可。 虽然 PopupWindow 对外没有暴露出 DecorView ,但只要是 PopupWindow 中的可见 View 都行。
public void showPopupWindow(int x, int y, int width, int height, View anchor){
if (mPopupWindow != null){
mPopupWindow.setFocusable(false);
mPopupWindow.update();
mPopupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, windowPos[0], windowPos[1]);
fullScreenImmersive(mPopupWindow.getContentView());
mPopupWindow.setFocusable(true);
mPopupWindow.update();
}
}
复制代码