Immersion status bar is the source of many mobile phone is the entity button, there is no virtual key, so open the immersion mode is only the status bar disappeared. The immersion mode becomes the immersion status bar.
Let's take a look at the actual effect.
After opening immersion mode, the status bar disappears, slides down from the top, the status bar appears, exits the immersion mode, and the status bar appears.
Our code is based on the previous article. First, two functions that open immersion mode and close immersion mode
@SuppressLint ("Newapi") public
static void Hidesystemui (view view) {
view.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);
}
@SuppressLint ("Newapi") public
static void Showsystemui (view view) {
view.setsystemuivisibility (
View.system_ui_flag_layout_stable
| View.system_ui_flag_layout_hide_navigation
| View.system_ui_flag_layout_fullscreen);
}
This code can be found in Google's developer documentation, which can be seen using immersive full-screen Mode, where the code will take effect in Android 4.4, and the corresponding Android version of the compatible judgment should be handled on your own.
In addition, an auxiliary function is needed to obtain the status bar height, using reflection.
/**
* Received status bar height
*
* @param context
* @return Notification Bar height
/public
int Getstatusbarheight context) {
int statusbarheight = 0;
try {
class<?> clazz = Class.forName ("Com.android.internal.r$dimen");
Object obj = clazz.newinstance ();
Field field = Clazz.getfield ("Status_bar_height");
int temp = Integer.parseint (Field.get (obj). toString ());
Statusbarheight = Context.getresources (). Getdimensionpixelsize (temp);
catch (Exception e) {
e.printstacktrace ();
}
return statusbarheight;
}
Click the Hide button to enter immersion mode, that is, hide the status bar, hide the status bar at the same time we need to modify the toolbar on the inside margin, otherwise it will look very ugly, here to register a listening Onsystemuivisibilitychangelistener, When we enter the immersion mode, we change the upper and toolbar margins of the
Hide.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (view v) {
View view = GetWindow (). Getdecorview ();
Hidesystemui (view);
Mtoolbar.setonsystemuivisibilitychangelistener (New View.onsystemuivisibilitychangelistener () {
@Override Public
void Onsystemuivisibilitychange (int visibility) {
mtoolbar.setpadding (mtoolbar.getpaddingleft (), 0, Mtoolbar.getpaddingright (), Mtoolbar.getpaddingbottom ());
}
})
;
After entering the immersion mode, the finger is drawn down from the top of the screen, and the status bar appears, and in about 2 seconds it disappears automatically.
Click on the Show button to exit the immersion mode, while the toolbar inner margin also increases to the height of the status bar.
Show.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (view v) {
View view = GetWindow (). Getdecorview ();
Showsystemui (view);
Mtoolbar.setonsystemuivisibilitychangelistener (New View.onsystemuivisibilitychangelistener () {
@Override Public
void Onsystemuivisibilitychange (int visibility) {
mtoolbar.setpadding (Mtoolbar.getpaddingleft (), Getstatusbarheight (Mainactivity.this), Mtoolbar.getpaddingright (), Mtoolbar.getpaddingbottom ());}}
});
See the effect of the above picture.
If you are using the Systembartintmanager of this class for the coloring of the status bar, in addition to the above operation, but also in the corresponding listening to increase the status bar coloring of the prohibition and start function.
Enter immersion mode, to disable
Tintmanager.setstatusbartintenabled (FALSE);
Exit immersion mode, to start
Tintmanager.setstatusbartintenabled (TRUE);
If you want to be more smooth, you can change the padding to add animation, the specific animation effect added itself.
Remember to judge the Android version before using immersion mode. System_ui_flag_immersive_sticky can only be used in greater than or equal to API level 19. You want to be compatible with the low version while using immersion mode. Use android.os.Build.VERSION.SDK_ before using System_ui_flag_immersive_sticky int to determine if the current system version is android4.4 or above, and if so, enable code, or skip if it is not.
The above is the entire content of this article, I hope you like.