简体   繁体   English

Android:检测导航栏的可见性

[英]Android: detect navigation bar visibility

How can I detect the presence of the navigation bar and hide it? 如何检测导航栏的存在并将其隐藏?

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 . 在我的onCreate()我调用hideNavigationBar()方法来隐藏导航栏,然后我注册一个监听器,以便每当用户触摸屏幕上任何位置时都可以看到导航栏,如文档所报告的那样。 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. 当触摸事件后导航栏变为可见时,侦听器再次调用hideNavigationBar()方法,但它没有效果,该栏仍然可见。

This is my onCreated() method: 这是我的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();
                        }
                    }
                });
    }

and this is my hideNavigationBar() method: 这是我的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? 如何在每次可见时隐藏导航栏?

Thanks 谢谢

you could add this code to your activity's onCreate() method: 您可以将此代码添加到您的活动的onCreate()方法中:

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

It's generally good practice to keep your UI in sync with changes in system bar visibility. 将UI与系统栏可见性的更改保持同步通常是一种很好的做法。 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 - 响应UI可见性更改

Do this: 做这个:

boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

if(!hasMenuKey && !hasBackKey) {
    // Do whatever you need to do, this device has a navigation bar
}

Original answer Check for navigation bar 原始答案检查导航栏

Try calling hideNavigationBar() in a Runnable using View.post(Runnable) . 尝试使用View.post(Runnable)Runnable调用hideNavigationBar() View.post(Runnable) For example: 例如:

...
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
    decorView.post(new Runnable() {
        @Override
        public void run() {
            hideNavigationBar();
        }
    });
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM