1. Demo
On Android 4.4 There is a new feature that sets the background of the phone's status bar, keeping the style of the phone's entire interface consistent, which is the default for Android L on Google I/O this year. Check out the demo first:
Figure one: Transparent status bar Figure 2: Blue Background status bar
2, the entire demo project structure diagram is as follows:
3. The main implementation code is as follows:
First, remember to import the Systembartint-1.0.4.jar package
The code in 3.1 Mainactivity.java is as follows:
PackageCom.example.testdemo;ImportAndroid.annotation.TargetApi;Importandroid.app.Activity;ImportAndroid.os.Build;ImportAndroid.os.Bundle;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportCom.readystatesoftware.systembartint.SystemBarTintManager; Public classMainactivityextendsActivity {Private BooleanIsbartint =true; Private BooleanIstranslucentbartint =false; Public voidSetbartint (Booleanisbartint) { This. Isbartint =Isbartint; } Public voidSettranslucentbartint (Booleanistranslucentbartint) { This. Istranslucentbartint =Istranslucentbartint; } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); if(isbartint) {Initsystembar (); } Else if(istranslucentbartint) {Inittranslucentsystembar (); }} @TargetApi (Build.version_codes. KITKAT)Private voidInitsystembar () {if(Build.VERSION.SDK_INT >=Build.version_codes. KITKAT) {Settranslucentstatus (true); Systembartintmanager Tintmanager=NewSystembartintmanager ( This); //activating the status bar settingsTintmanager.setstatusbartintenabled (true); //activating navigation bar SettingsTintmanager.setnavigationbartintenabled (false); intSBG =getresources (). GetColor (R.color.titlebar_rl_background); Tintmanager.settintcolor (SBG); Tintmanager.setstatusbartintcolor (SBG); }} @TargetApi (Build.version_codes. KITKAT)Private voidInittranslucentsystembar () {if(Build.VERSION.SDK_INT >=Build.version_codes. KITKAT) {Settranslucentstatus (true); Systembartintmanager Tintmanager=NewSystembartintmanager ( This); //activating the status bar settingsTintmanager.setstatusbartintenabled (true); //activating navigation bar SettingsTintmanager.setnavigationbartintenabled (false); intSBG =getresources (). GetColor (R.color.viewfinder_title); Tintmanager.settintcolor (SBG); Tintmanager.setstatusbartintcolor (SBG); }} @TargetApi (Build.version_codes. KITKAT)Private voidSettranslucentstatus (BooleanIsOpen) {Window win=GetWindow (); Windowmanager.layoutparams Winparams=win.getattributes (); Final intBITS =WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if(isOpen) {winparams.flags|=bits; } Else{winparams.flags&= ~bits; } win.setattributes (Winparams); } @TargetApi (Build.version_codes. KITKAT)Private voidSettranslucentnavigation (BooleanIsOpen) {Window win=GetWindow (); Windowmanager.layoutparams Winparams=win.getattributes (); Final intBITS =WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; if(isOpen) {winparams.flags|=bits; } Else{winparams.flags&= ~bits; } win.setattributes (Winparams); }}
The color values for the 3.2 colors.xml are as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <Colorname= "Titlebar_rl_background">#33b5e6</Color> <Colorname= "Viewfinder_title">#80000000</Color></Resources>
The code in 3.3 Styles.xml is as follows:
<Resources> <stylename= "Apptheme"Parent= "Appbasetheme"> <Itemname= "Android:windownotitle">True</Item> <Itemname= "Android:fitssystemwindows">True</Item> </style></Resources>
3.4 androidmanifest.xml application or corresponding activity settings theme Android:theme= "@style/apptheme"
(v) Set the background of the phone status bar on Android 4.4