Original: Android Project Combat (41): Game and video type apply status bar immersive effect
Demand:
Mobile app, when playing a game or full screen watching video will find that this time the top of the mobile phone status bar is not displayed, when we swipe down from the top of the phone or the bottom of the phone to swipe up, the status bar will be displayed, if a short period of time without action, 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 >=) { //If there is focus, description is currently interacting with the user and sdk_int>=19 only android4.4+ In order to support 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) method
Activity life cycle, can be understood as the way to go to this method, the activity is the real meaningof the display/truefalse
2, Getwindow.getdecorview () method, get the top view of window interface
Decorview is the topmost view of the entire window interface. Second, Decorview only one child element is linearlayout. Represents the entire window interface, including the notification bar, the title bar, the content display bar three block area. There are two framelayout elements in LinearLayout. Display the interface for the title bar. Only one TextView displays the name of the app. You can also customize the title bar, and the post-loaded custom title bar view will be added to the framelayout. Display the interface for the content bar. The Setcontentview () method loads the layout interface and joins it.
Note:
and activity full screen display difference is that if the phone has a virtual navigation bar (ie, virtual back, home button), the full screen will always be displayed, and the above method, the navigation bar and status bar synchronization display, the above requirements with full-screen implementation of the effect is not correct.
Android Project Combat (41): Game and video type apply status bar immersive effects