Android project combat (forty-one): game and video type application status bar immersive effect

Original: Android project combat (forty-one): game and video type application status bar immersive effect

  need:

   Mobile app, when playing games or watching videos in full screen, you will find that the status bar at the top of the phone is not displayed. When we slide down from the top of the phone or slide up from the bottom of the phone, the status bar will be displayed. If there is no operation for a few seconds, the status bar will be hidden again.

  

  Implementation code:

 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
  }

  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   if (hasFocus && Build. VERSION.SDK_INT >= 19) { // If there is focus, it means that it is currently interacting with the user and SDK_INT>=19 Only Android4.4+ supports immersive effects
   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
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
   }
  }
 

   

  Effect:

          

 

  Code Explanation:

  1、 onWindowFocusChanged(boolean hasFocus) 方法

     The Activity life cycle can be understood as when this method is reached, the activity is actually displayed/ disappeared in the interaction layer.

    When the activity is displayed at the top of the stack and interacts with the user, it has focus and hasFocus is true

    When the activity exits the top of the stack and does not interact with the user, it has no focus and hasFocus is false

 

 

  2. The getWindow.getDecorView() method gets the topmost View of the Window interface

  1. DecorView is the topmost View of the entire Window interface.

  Second, DecorView has only one child element LinearLayout. Represents the entire Window interface, including three areas: notification bar, title bar, and content display bar.

  3. There are two FrameLayout child elements in LinearLayout.

    ( 20 ) is the title bar display interface. There is only one TextView showing the name of the app. You can also customize the title bar, and the loaded custom title bar View will be added to the FrameLayout.

    ( 21 ) is the content bar display interface. It is the layout interface loaded by the setContentView() method and added to it.

 

 

    

    Note:

    The difference from Activity full-screen display is that if the phone has a virtual navigation bar (that is, virtual back, home button), the full-screen display will always be displayed, while the above method, the navigation bar and the status bar are displayed synchronously, and the above requirements are not implemented in full-screen.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325112710&siteId=291194637