沉浸式显示效果,这是在 Android 4.4 (API Level 19) 引入的一个概念,日常开发中经常能接触到,官方介绍 Using Immersive Full-Screen Mode 。

Android 的 System bars 包含 Status bar 和 Navigation bar,如下图所示顶部状态栏是 Status bar,底部虚拟按键栏是 Navigation bar。

沉浸式全屏显示效果

通常 System Bars 会和你的应用布局同时显示在屏幕上。应用为了可以沉浸式显示内容,可以通过暂时淡化 System bars 来实现减少分散用户注意力的体验,或者通过暂时隐藏 System bars 来实现完全沉浸式的的体验。具体可参考这篇文章 管理 System window bars

在日常开发中,我们主要是处理 Activity 的沉浸效果,在全屏沉浸模式中弹出 Dialog 或 PopupWindow,则会自动退出沉浸模式。退出沉浸式的原因是因为 Activity 的 Window 焦点被抢走了,Window 中的 DecorView 状态改变导致了退出。

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 结构。

可以看到不论是系统状态栏或是虚拟按键栏都是附在 DecorView 上,要避免 DecorView 状态改变而导致沉浸式模式退出。除了设置在 Activity 的 onWindowFocusChanged 方法状态时重新设置进入沉浸式模式,还需要把 Dialog 和 PopupWindow 弹出时造成的焦点改变也考虑进去。

下面给出在 Activity 、Dialog 和 PopupWindow 中的沉浸式效果的具体实现。

在 Activity 中实现

@Overrideprotected 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);}@Overridepublic 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 中实现

Dialog 在初始化时会生成新的 Window,先禁止 Dialog Window 获取焦点,等 Dialog 显示后对 Dialog Window 的 DecorView 设置 setSystemUiVisibility ,接着再获取焦点,这样看起来就没有退出沉浸模式。

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 中实现

而 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();    }}

更多相关文章

  1. Android弹出式对话框AlertDialog中的EditText自动打开软键盘
  2. Android再按一次完全退出程序代码
  3. android 4.4 沉浸式状态栏实现
  4. android多activity下如何退出整个程序
  5. android 完全退出实现
  6. Android实现沉浸式(透明)状态栏适配
  7. Android(安卓)退出多个Activity | 退出程序
  8. Android完全退出App的一些问题
  9. 什么是Activity

随机推荐

  1. go是强类型语言么
  2. 方便好用的Golang配置库(Viper)
  3. 总结Golang实现PHP常用函数
  4. go语言如何引入包
  5. 详解GoLang实现基于gin+jaeger的opentrac
  6. go-carbon1.2.5版本发布,新增两个互转方法
  7. 解决golang中vendor引起的相同类型,却提示
  8. 你知道为啥不再建议使用GOPATH以及如何使
  9. go语言有goto吗
  10. go语言都用什么框架