Android Color status bar

Source: Internet
Author: User

The app you've been thinking about making yourself look good. So add a status bar to your app that can change color. I did not do the downward compatibility ... Only mobile phone version is 4.4 and above the use of mobile phones. Someone on the internet is called immersive, and I think the blogger's explanation is right here:
Android Implementation Color Status bar

A particularly important step:
Activity must use a theme!

android:theme="@style/AppTheme.NoActionBar"

Here I am using the default theme to see what you need to use.

1, first in the activity's OnCreate method, set the title bar to Transparent:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,         WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        }

2, and then to keep your top control moving up, you need to add the following two sentences to the top control of the layout file:

 android:fitsSystemWindows="true" android:clipToPadding="true"

3, then if the background color of your top control is blue, then your title bar will also turn blue:
You can look at the following:

Share:
Here is a simple book on the Great God wrote a tool Class I used very good ... Share to everyone. Also thank the great God for sharing:
Need to download an open Source Library yourself Systembartint
Have time I also want to this open source library source code to look, study!

 Public  class statusbarutil {    /** * Modify the status bar to full transparency * * @param activity */    @TargetApi( +) Public Static void Transparencybar(Activity activity) {if(Build.VERSION.SDK_INT >= build.version_codes.            LOLLIPOP) {window window = Activity.getwindow (); 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); }Else if(Build.VERSION.SDK_INT >= build.version_codes.            KITKAT) {window window = Activity.getwindow (); Window.setflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLU        Cent_status); }    }/** * Modify the status bar color, support version 4.4 or above * * @param activity * @param colorid */     Public Static void Setstatusbarcolor(Activity activity,intColorid) {if(Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {window window = Activity.getwindow ();//Window.addflags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);Window.setstatusbarcolor (Activity.getresources (). GetColor (Colorid)); }Else if(Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {//using the Systembartint library to color the 4.4 status bar, you need to first set the status bar to TransparentTransparencybar (activity); Systembartintmanager Tintmanager =NewSystembartintmanager (activity); Tintmanager.setstatusbartintenabled (true); Tintmanager.setnavigationbartintenabled (true);        Tintmanager.setstatusbartintresource (Colorid); }    }/** * Set the status bar black font icon, * suitable for 4.4 or more versions miuiv, FlyMe and 6.0 or later other Android * * @param activity * @ret Urn 1:miuui 2:flyme 3:android6.0 * /     Public Static int Statusbarlightmode(Activity activity) {intresult =0;if(Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {if(Miuisetstatusbarlightmode (Activity.getwindow (),true) {result =1; }Else if(Flymesetstatusbarlightmode (Activity.getwindow (),true) {result =2; }Else if(Build.VERSION.SDK_INT >= build.version_codes.                    M) {Activity.getwindow (). Getdecorview (). Setsystemuivisibility (View.system_ui_flag_layout_fullscreen |                View.system_ui_flag_light_status_bar); result =3; }        }returnResult }/** * When the system type is known, set the status bar Black font icon. * Suitable for 4.4 or more versions miuiv, FlyMe and above 6.0 other Android * * @param activity * @param type 1:miuui 2:flym E 3:android6.0 * /     Public Static void Statusbarlightmode(Activity activity,intType) {if(Type = =1) {Miuisetstatusbarlightmode (Activity.getwindow (),true); }Else if(Type = =2) {Flymesetstatusbarlightmode (Activity.getwindow (),true); }Else if(Type = =3) {Activity.getwindow (). Getdecorview (). Setsystemuivisibility (View.system_ui_flag_layout_fullscreen | View.        System_ui_flag_light_status_bar); }    }/** * Clear MIUI or FlyMe or 6.0 or later status bar black font */     Public Static void Statusbardarkmode(Activity activity,intType) {if(Type = =1) {Miuisetstatusbarlightmode (Activity.getwindow (),false); }Else if(Type = =2) {Flymesetstatusbarlightmode (Activity.getwindow (),false); }Else if(Type = =3) {Activity.getwindow (). Getdecorview (). setsystemuivisibility (view.system_ui_flag_visible); }    }/** * Set status bar icons for dark and Meizu-specific text styles * can be used to determine if the window needs to be set for flyme user * * @param windows * @param Dark If the status bar font and icon color are set to dark * @return Boolean Successful execution returns true */     Public Static Boolean Flymesetstatusbarlightmode(Window window,BooleanDark) {Booleanresult =false;if(Window! =NULL) {Try{Windowmanager.layoutparams LP = Window.getattributes (); Field Darkflag = WindowManager.LayoutParams.class. Getdeclaredfield ("Meizu_flag_dark_status_bar_icon"); Field meizuflags = WindowManager.LayoutParams.class. Getdeclaredfield ("Meizuflags"); Darkflag.setaccessible (true); Meizuflags.setaccessible (true);intbit = Darkflag.getint (NULL);intValue = MEIZUFLAGS.GETINT (LP);if(Dark)                {Value |= bit; }Else{Value &= ~bit;                } meizuflags.setint (LP, value);                Window.setattributes (LP); result =true; }Catch(Exception e) {            }        }returnResult }/** * Set the status bar font icon to be dark, need to MIUIV6 above * * @param window needs to be set Windows * @param Dark Whether to put the status bar font and Icon Yan Color set to Dark * @return Boolean Successful execution returns true * /     Public Static Boolean Miuisetstatusbarlightmode(Window window,BooleanDark) {Booleanresult =false;if(Window! =NULL) {Class clazz = Window.getclass ();Try{intDarkmodeflag =0; Class layoutparams = Class.forName ("Android.view.miuiwindowmanager$layoutparams"); Field field = Layoutparams.getfield ("Extra_flag_status_bar_dark_mode");                Darkmodeflag = Field.getint (layoutparams); Method Extraflagfield = Clazz.getmethod ("Setextraflags",int. class,int. Class);if(Dark) {extraflagfield.invoke (window, Darkmodeflag, Darkmodeflag);//status bar transparent and black font}Else{Extraflagfield.invoke (window,0, Darkmodeflag);//Clear Black font} result =true; }Catch(Exception e) {            }        }returnResult }}

This tool class specific function of the author notes are also very detailed!
I wrote a small demo, need to download to see ~
Https://github.com/slyjs/StatusTintDemo.git

Need to learn a lot AH think tight!!!!

Android Color status bar

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.