android dynamically controls the status bar to show and hide method instances

  • 2020-05-19 05:49:49
  • OfStack

Method 1 :(the experiment failed, but it was widely circulated on the Internet. Maybe I used the method improperly, which needs to be further verified...)

One of the ways android wants to apply the full screen at runtime is to add the following code to activity's onCreat method: getWindow().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN); And it needs to be before setContentView (), otherwise it will be invalid. It can be seen from so many harsh conditions that this method cannot satisfy dynamic control.

The following method can meet this need. Call setSystemUiVisibility() for View

Method with the following parameters:


View.SYSTEM_UI_FLAG_FULLSCREEN,   // Full screen, the status bar and navigation bar are not displayed 
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION, // Hide navigation bar 
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, // Full screen, the status bar will be overlaid on the layout 
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION, 
View.SYSTEM_UI_FLAG_LAYOUT_STABLE,
View.SYSTEM_UI_FLAG_LOW_PROFILE,
View.SYSTEM_UI_FLAG_VISIBLE,  // Displays the status bar and navigation bar 
View.SYSTEM_UI_LAYOUT_FLAGS

Method 2 :(successful experiment)
Directly call this method to achieve the control of the status bar;


private void full(boolean enable) {
        if (enable) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            WindowManager.LayoutParams attr = getWindow().getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attr);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }


Related articles: