Programmatically enable/disable Immersive Mode

元气小坏坏 提交于 2019-11-30 15:13:02

问题


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 navigation bar permanently on a screen, but I want to know if this is possible. I have looked into rooting my device and using the Xposed framework.

Is there a way to programmatically disable the navigation bar, or "sticky mode", and re-enable later?

Edit: I've looked at the Android Immersive Mode but it seems like the navigation bar will still show if the user touches the edge. I want to remove any hint of the navigation bar until they go to my settings screen.


回答1:


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();


                    }
                });`


来源:https://stackoverflow.com/questions/38254127/programmatically-enable-disable-immersive-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!