Note: The company development task adaptation is performed on the 4.4 release, so this adaptation is only tested on top of 4.4.
1, the main use of third-party open source projects Systembartint,github:https://github.com/jgilfelt/systembartint
2, according to Systembartint to carry out the study of sample, the main steps are as follows:
- Add the following code to the activity:
Public classMainactivityextendsbaseactivity {@Override Public voidSetcontentview () {Setcontentview (r.layout.activity_main); //Immersive status bar Settranslucentstatus (); }
}/*** Add android:fitssystemwindows= "true" property for the root layout of XML <br/>*/ protected voidSettranslucentstatus () {if(Build.VERSION.SDK_INT >=Build.version_codes. KITKAT) {Settranslucentstatus (true); } Systembartintmanager Tintmanager=NewSystembartintmanager ( This); Tintmanager.setstatusbartintenabled (true); Tintmanager.setstatusbartintresource (R.COLOR.STATUSBAR_BG);//the color required for the notification bar} @TargetApi (19) Private voidSettranslucentstatus (BooleanOn ) {Window win=GetWindow (); Windowmanager.layoutparams Winparams=win.getattributes (); Final intBITS =WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if(ON) {Winparams.flags|=bits; } Else{winparams.flags&= ~bits; } win.setattributes (Winparams); }
Note: Some data say that the Settranslucentstatus () method needs to be placed after Setconentview (), and actually has no effect.
2. In the root layout of mainactivity, add:
Android:fitssystemwindows= "true"
3. If we add a custom TextView control to the middle of the layout and set the background color to be the same as the color in the Settranslucentstatus () method, we will find the effect as:
Among them, in the middle of StatusBar and TextView there will be a blank, this blank is Actionbar white, so, if the use of titlebar, you want to remove the actionbar display; If you use Actionbar, To set the background of the actionbar to the same color.
Public class mainactivity {@Override Public void Setcontentview () { notitlebar (); Setcontentview (R.layout.activity_main); // immersive status bar settranslucentstatus (); } /** * Does not display the title bar * / protectedvoid Notitlebar () { this. Requestwindowfeature (Window.feature_no_title); }}
The effect is as follows:
4. Since the code above GitHub is based on Gradle, the release source based on Eclipse is released below.
Android All-in-one (immersive) status bar implementation