Android:检测导航栏可见性 [英] Android: detect navigation bar visibility

查看:58
本文介绍了Android:检测导航栏可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测导航栏的存在并将其隐藏?

How can I detect the presence of the navigation bar and hide it?

在我的 onCreate()中,我调用 hideNavigationBar()方法来隐藏导航栏,然后我注册一个侦听器以在每次导航栏可见时隐藏导航栏.用户触摸了文档所报告的屏幕上的任意位置.当触摸事件后导航栏变得可见时,侦听器会再次调用 hideNavigationBar()方法,但该方法没有效果,仍然可见.

In my onCreate() I call hideNavigationBar() method to hide the navigation bar, then I register a listener to hide the navigation bar every time it becomes visible when the user touches anywhere on the screen as reported by the documentations. When the navigation bar becomes visible after a touch event the hideNavigationBar() method is called again by the listener, but it has not effect, the bar is still visible.

这是我的 onCreated()方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hideNavigationBar();

        View decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            Toast.makeText(getApplicationContext(), "Visible", Toast.LENGTH_SHORT).show();
                            hideNavigationBar();
                        } else {
                            Toast.makeText(getApplicationContext(), "Not visible", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

这是我的 hideNavigationBar()方法:

 private void hideNavigationBar() {

        Toast.makeText(getApplicationContext(), "hideNavigationBar()", Toast.LENGTH_SHORT).show();

        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }

如何在每次看到导航栏时将其隐藏?

How can I hide the navigation bar every time it becomes visible?

谢谢

推荐答案

您可以将此代码添加到活动的onCreate()方法中:

you could add this code to your activity's onCreate() method:

      View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {                
                        // TODO: The navigation bar is visible. Make any desired
                        // adjustments to your UI, such as showing the action bar or
                        // other navigational controls.
                        hideNavigationBar() 

                    } else {
                        // TODO: The navigation bar is NOT visible. Make any desired
                        // adjustments to your UI, such as hiding the action bar or
                        // other navigational controls.
                    }
                }
            });

通常最好的做法是使UI与系统栏可见性的更改保持同步.例如,您可以使用此侦听器来隐藏和显示操作栏,以及隐藏和显示状态栏.

It's generally good practice to keep your UI in sync with changes in system bar visibility. For example, you could use this listener to hide and show the action bar in concert with the status bar hiding and showing.Android-Responding to UI Visibility Changes

这篇关于Android:检测导航栏可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆