Programmatically enable/disable Immersive Mode

前端 未结 1 605
死守一世寂寞
死守一世寂寞 2020-12-31 17:12

I\'m looking to make my Android app fullscreen, but only showing the android navigation bar on a certain screen (my settings screen). I know that it is dangerous to hide the

相关标签:
1条回答
  • 2020-12-31 17:17

    Yes it is possible. Use the below code snippet to achieve the desired functionality.

    // 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.
       View decorView = getWindow().getDecorView();
        decorView.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() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
    

    For more details refer the the below google documentation :

    https://developer.android.com/training/system-ui/immersive.html

    EDIT 1 : To hide it permanently may be you could try something like this (Hacky)

    decorView.setOnSystemUiVisibilityChangeListener
                    (new View.OnSystemUiVisibilityChangeListener() {
                        @Override
                        public void onSystemUiVisibilityChange(int visibility) {
    
                                hideSystemUI();
    
    
                        }
                    });`
    
    0 讨论(0)
提交回复
热议问题