Application of immersive status bar in Android

Source: Internet
Author: User

After the Android5.0 version, Google has added many new features to the Android system, refreshing the Android user experience. And one of the new features is the immersive status bar. So the problem is, a lot of non-mobile partners will ask, what is the immersive status bar? The traditional mobile phone status bar is shown in black strips, and some of the main interface of the mobile phone has a very obvious difference. In this way, the visual width is sacrificed to a certain extent, and the interface area becomes smaller. The immersive status bar turns the color of the status bar Transparent, and the original layout is stained with the phone screen, making the status bar and the phone layout content integrated. Below, let's elaborate on the actual application of the immersive status bar in the project.

To implement immersion in the current activity interface, first we will write the following code in the OnCreate method:

 getwindow (). Requestfeature (Window.feature_no_title); Window window  = GetWindow ();  if  (Build.VERSION.SDK_INT >= Build.version_codes. LOLLIPOP) {window.clearflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS  | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); Window.getdecorview (). setsystemuivisibility (view.system_ui_flag_layout_fullscreen  | view.system_ui_flag_layout_hide_navigation  |    view.system_ui_flag_layout_stable);   Window.addflags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);   Window.setstatusbarcolor (color.transparent); Window.setnavigationbarcolor (color.transparent);}  

This allows for immersive state of the code. Since the immersive status bar only supports more than 5.0 phones, we have made version judgments here. However, in practice, small partners have encountered new problems. Some of the virtual keys are also immersed in, this will be very depressed. Our idea is that there is a black view in the layout, which is displayed on a mobile phone with a virtual key, and the height is consistent with the virtual key. Here's how.

Check if you have a virtual key

 Public Static BooleanHasnavbar (Context context) {Resources res=context.getresources (); //this way must pay attention to the correct wording, internal should be through reflection to call.         intResourceId = Res.getidentifier ("Config_shownavigationbar", "bool", "Android"); if(ResourceId! = 0) {            BooleanHasnav =Res.getboolean (resourceId); //Check override flagString snavbaroverride =getnavbaroverride (); if("1". Equals (Snavbaroverride)) {Hasnav=false; } Else if("0". Equals (Snavbaroverride)) {Hasnav=true; }            returnHasnav; } Else{//fallback            if(Build.VERSION.SDK_INT >= 14) {                return!viewconfiguration.get (context). Haspermanentmenukey (); } Else {                return false; }        }    }

Determine if the virtual key bar overrides

Private Staticstring Getnavbaroverride () {string Snavbaroverride=NULL; if(Build.VERSION.SDK_INT >=Build.version_codes. KITKAT) {Try{Class C= Class.forName ("Android.os.SystemProperties"); Method m= C.getdeclaredmethod ("Get", String.class); M.setaccessible (true); Snavbaroverride= (String) M.invoke (NULL, "Qemu.hw.mainkeys"); } Catch(Throwable e) {}}returnSnavbaroverride; }

Get the virtual key bar height

 Public Static int Getbottomstatusheight (Context context) {        int totalheight = getdpi (context);         int contentheight = getscreenheight (context);         return totalheight- contentheight;    }

Get screen height

 Public Static int Getscreenheight (Context context) {        = (windowmanager) context                . Getsystemservice (Context.window _service);         New displaymetrics ();        Wm.getdefaultdisplay (). Getmetrics (outmetrics);         return outmetrics.heightpixels;    }
Get the screen's original size height, including the virtual function key height
 Public Static intgetdpi (Context context) {intDPI = 0; WindowManager WindowManager=(WindowManager) Context.getsystemservice (Context.window_service); Display Display=Windowmanager.getdefaultdisplay (); Displaymetrics Displaymetrics=NewDisplaymetrics (); @SuppressWarnings ("Rawtypes") Class C; Try{C= Class.forName ("Android.view.Display"); @SuppressWarnings ("Unchecked") Method Method= C.getmethod ("Getrealmetrics", Displaymetrics.class);            Method.invoke (display, displaymetrics); DPI=Displaymetrics.heightpixels; } Catch(Exception e) {e.printstacktrace (); }        returndpi; }

Set the height of the view in the layout in the code

if(Hasnavbar ( This)) {bottomview.getviewtreeobserver (). Addongloballayoutlistener (NewViewtreeobserver.ongloballayoutlistener () {//callback This method when layout execution finishes@Override Public voidongloballayout () {linearlayout.layoutparams params= (linearlayout.layoutparams) bottomview.getlayoutparams (); Params.height = Getbottomstatusheight (activityshopindex. This);                            Bottomview.setlayoutparams (params);                        Bottomview.setvisibility (view.visible);        }                    }); }

This allows us to successfully solve the problem of the virtual key bar being immersed.

Author of Water and Wood

Application of immersive status bar in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.