Android:检测导航栏的可见性

时间:2015-04-21 14:50:58

标签: android

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

在我的onCreate()我调用hideNavigationBar()方法来隐藏导航栏,然后我注册一个监听器,以便在用户触摸屏幕上的任何位置时每次显示时隐藏导航栏。 documentations。当触摸事件后导航栏变为可见时,侦听器再次调用hideNavigationBar()方法,但它没有效果,条形图仍然可见。

这是我的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);
    }

每次导航栏变为可见时如何隐藏导航栏?

由于

3 个答案:

答案 0 :(得分:9)

您可以将此代码添加到您的活动的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.
                    }
                }
            });

将UI与系统栏可见性的更改保持同步通常是一种很好的做法。例如,您可以使用此侦听器隐藏并显示与状态栏隐藏和显示一致的操作栏。Android-Responding to UI Visibility Changes

答案 1 :(得分:1)

这样做:

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
}

原始答案Check for navigation bar

答案 2 :(得分:0)

尝试使用View.post(Runnable)hideNavigationBar()中呼叫Runnable。例如:

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