Android 沉浸式状态栏

概述

在android 中关于Status Bar和Navigation Bar 的处理,主要有透明栏(Translucent) 和 沉浸栏(Immersive)

Immersive

参考:与Status Bar和Navigation Bar相关的一些东西
Using Immersive Full-Screen Mode

全屏模式,通常用在 游戏 或者阅读类的 app 中,Full-screen Immersive Mode 分为两种,Lean Back(后撤式 )Immersive (沉浸式)

这两种的主要区别是:http://developer.android.com/intl/zh-cn/design/patterns/fullscreen.html

Lean Back
Touch the screen anywhere to bring back system bars.

Immersive
Swipe from the any edge of the screen with a hidden bar to bring back system bars.

在android 4.4 中,Immersive 又分为两种,

View.SYSTEM_UI_FLAG_IMMERSIVE
退出IMMERSIVE模式 后需要手动 控制再次 进入Immersive 模式

View.SYSTEM_UI_FLAG_IMMERSIVE_STICK
退出Immersive模式显示SystemUI 一段时间后隐藏,且不会调用onSystemUiVibilityChanged

关于 Full_screen体验,github上已有开源项目SystemUiHelper.java

实现方式

  • Android 实现沉浸式状态栏
    主要是用的是
// This snippet hides the system bars.
private void hideSystemUI() {
    // Set the IMMERSIVE flag.
    // Set the content to appear under the system bars so that the content
    // doesn't resize when the system bars hide and show.
    mDecorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
            | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
    mDecorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
  • 使用SystemUiHelper.java

    主要使用了如下代码

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

        final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        SystemUiHelper uiHelper =  new SystemUiHelper(this, SystemUiHelper.LEVEL_IMMERSIVE ,flags);
        uiHelper.hide();
}

你可能感兴趣的:(沉浸状态栏,Immersive)