How to hide the navigation bar in Android 6.0?

2019-05-23 10:06发布

I have the following code:

getWindow().getDecorView().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
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

This code worked fine for Android Lollipop, hiding the navigation bar in sticky immersive mode. But now, when I test it on my phone with Android 6.0, the navigation bar goes away while a black rectangle where the navigation bar used to be remains, blocking a portion of the screen.

2条回答
对你真心纯属浪费
2楼-- · 2019-05-23 10:30

Looking back on my question, I'd like to add the solution I'm using now which hasn't failed me since, I don't remember if I saw it somewhere else or came to it myself but I'm glad it works.

public static void activiateFullscreen(Activity activity){
    View decorView = activity.getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    if (Build.VERSION.SDK_INT >= 17) {
        uiOptions ^= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                |View.SYSTEM_UI_FLAG_LOW_PROFILE;
    }
    if (Build.VERSION.SDK_INT >= 19) {
        uiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    }
    decorView.setSystemUiVisibility(uiOptions);
}

Hope this helps someone!

查看更多
【Aperson】
3楼-- · 2019-05-23 10:30

I've discovered a workaround that seems to resolve this issue. I support portrait and landscape, and noticed that the black rectangle went away if I rotated into landscape or started the app in landscape. Adding the following code to my main activity's onCreate() method (after setting the immersive flags) resolved the issue:

    if (Build.VERSION.SDK_INT >= 23) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }
查看更多
登录 后发表回答