• Android系统栏(状态栏、导航栏)隐藏


    Managing System UI

    标签: UI SystemBar


    调暗系统Bars

    Android4.0(API level 14)及以上使用
    此方法不会导致内容大小的变化,它只是简单的将系统Bars(状态栏跟导航栏)模糊处理,一旦用户点击屏幕,将会恢复正常。目的是为了让用户更加专注于APP的内容中。
    当点击导航栏、导航栏上滑、状态栏下滑、弹出菜单等操作时,标志被清除。
    

    使用:

    View decorView = getActivity().getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
    decorView.setSystemUiVisibility(uiOptions);
    

    恢复也很简单,只需将uiOptions置为0

    View decorView = getActivity().getWindow().getDecorView();
    // Calling setSystemUiVisibility() with a value of 0 clears
    // all flags.
    decorView.setSystemUiVisibility(0);
    
    此方式会引起 onSystemUiVisibilityChange()的回调
    

    隐藏状态栏

    • Android 4.0及以下

      • Android4.0及以下通过设置WindowManager标志完成,具体可以通过清单中设置FullScreen主题,比如:
      <application
      ...
      android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
      ...
      
    ``` - 也可以通过代码动态设置,如下:
    ```
    if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    //注意:通过此方式设置的标志需要手动清除,否则会一直作用,并且onSystemUiVisibilityChange()不会被回调
    ```
    
    
    
    >通过清单配置的好处是UI过渡流畅,因为系统在Activity实例化之前就已经获取到渲染UI的信息。
    
    • Android 4.1及以上

    Android4.1及以上隐藏状态栏是通过调用setSystemUiVisibility()方法 ,需要注意的是,隐藏状态栏的时候需要把ToolBar也隐藏了

    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    // Remember that you should never show the action bar if the
    // status bar is hidden, so hide that too if necessary.
    ActionBar actionBar = getActionBar();
    actionBar.hide();
    

    注意通过此方式隐藏状态栏,当按下home键或者弹出对话框等,标志会被清除,onSystemUiVisibilityChange()会被调用,点击屏幕不会清除标志


    隐藏导航栏

    Android4.0(API level 14)
    通常来说,在隐藏导航栏的时候,需要一并将状态栏隐藏。

    View decorView = getWindow().getDecorView();
    // Hide both the navigation bar and the status bar.
    // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
    // a general rule, you should design your app to hide the status bar whenever you
    // hide the navigation bar.
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                  | View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    

    注意此时点击屏幕、菜单、home键、显示Dialog等操作,标记即被清,onSystemUiVisibilityChange()会被调用


    沉浸模式

    Android4.4(API level 19)引入 SYSTEM_UI_FLAG_IMMERSIVE,这个标志可以让我们真正实现全屏模式,为什么这么说呢?因为前面隐藏导航栏跟状态栏时,点击屏幕后标志就会被清除,而在这三者配合使用后,可以使我们捕获屏幕所有的触碰事件,此时点击屏幕并不会清除标志,除非我们从状态栏下滑,或者从导航栏上滑,这才会清除标志,并且会触发onSystemUiVisibilityChange()回调
    另外,如果我们需要在标志被清除后可以自动再次全屏,将SYSTEM_UI_FLAG_IMMERSIVE替换为SYSTEM_UI_FLAG_IMMERSIVE_STICKY即可,但是需要注意的是,由于使用此标志之后,系统Bars只是在半透明状态下短暂地显示,所以并不会触发onSystemUiVisibilityChange()回调

    用法:

    // 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);
    }
    

    添加回调

    通过给View注册View.OnSystemUiVisibilityChangeListener来响应UI变化
    前面多次提及的onSystemUiVisibilityChange()便是View.OnSystemUiVisibilityChangeListener接口中的方法

    View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            // Note that system bars will only be "visible" if none of the
            // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                // TODO: The system bars are visible. Make any desired
                // adjustments to your UI, such as showing the action bar or
                // other navigational controls.
            } else {
                // TODO: The system bars are NOT visible. Make any desired
                // adjustments to your UI, such as hiding the action bar or
                // other navigational controls.
            }
        }
    });
    
  • 相关阅读:
    更改ubuntu的官方镜像源
    python中的decorator的作用
    thunderbird 设置 邮件回复时内容在上方显示
    Sapnco3 RfcTable Structure
    DbEntry 访问Access2010数据库
    DbEntry 默认 主键ID为long
    DbEntry 简单实现
    nginx学习时使用EditPuls编辑conf.xml
    java的集合类面试题
    tomcat组成介绍和调优方案
  • 原文地址:https://www.cnblogs.com/caoweizhao/p/6788914.html
Copyright © 2020-2023  润新知