(v) Set the background of the phone status bar on Android 4.4

Source: Internet
Author: User
Tags getcolor

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

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.