Detecting when system buttons are visible while using 'immersive mode'

后端 未结 4 744
谎友^
谎友^ 2020-12-28 08:39

I\'m currently using immersive mode (API 19) for one of my Activities as follows:

getWindow().getDecorView()
            .setSystemUiVisibility(
                     


        
4条回答
  •  萌比男神i
    2020-12-28 09:12

    UPDATED ANSWER:

    Set an OnSystemUiVisibilityChangeListener, force immersive mode when visibility is 0 (rather than 6).

    if(android.os.Build.VERSION.SDK_INT >= 19) {
            getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    if(visibility == 0) {
                        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);
                    }
                }
            });
        }
    

    OLD, NASTY ANSWER:

    this is nasty, but it is a solution IFF you were showing an ActionBar:

    • add an OnGlobalLayoutListener to the ViewTreeObserver of 'action_bar_container'.
    • in the OnGlobalLayoutListener implementation, check if 'action_bar_container' visibility if GONE or not.
    • when it moved from GONE to !GONE (and assuming you were in immersive mode) then force immersive mode again via the setSystemUiVisibility method.

    if(android.os.Build.VERSION.SDK_INT >= 19) {
            int actionBarContainerId = Resources.getSystem().getIdentifier("action_bar_container", "id", "android");
            ((ViewGroup)findViewById(actionBarContainerId)).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int actionBarContainerId = Resources.getSystem().getIdentifier("action_bar_container", "id", "android");
                    ViewGroup actionBarContainer = (ViewGroup) findViewById(actionBarContainerId);
                    if(actionBarContainer.getVisibility() == View.GONE) {
                        if(DEBUG) Log.d(TAG, "...PROBABLY IN IMMERSIVE MODE AND ALL IS GOOD!..");
                    } else {
                        if(DEBUG) Log.d(TAG, "...PROBABLY NO LONGER IN IMMERSIVE MODE, HEY..");
                        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);
                    }
    
                }
            });
        }
    

提交回复
热议问题