天天看點

Android 五步修改狀态欄顔色

在 官方文檔 中介紹了可以引用v21的相容包,在樣式中配置如下主題樣式就可以達到我們的目的

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- Here we setting appcompat’s actionBarStyle -->
    <item name="actionBarStyle">@style/MyActionBarStyle</item>
    <!-- ...and here we setting appcompat’s color theming attrs -->
    <item name="colorPrimary">@color/my_awesome_red</item>
    <item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
    <!-- The rest of your attributes -->
</style>
           
colorPrimaryDark 就是用于設定狀态欄顔色的。但坑爹的是我按照文檔寫了一個demo,但運作後狀态欄顔色就是不改變,一度以為是我demo寫的有問題。上stackoverflow查了很久後發現不僅僅是我才遇到這個問題,這個可能是個小bug。總之想要現在通過v21這個包來實作在5.0以前的版本狀态欄顔色變化是不可能的了。于是乎我google了好久,好消息是這個特性可以實作,壞消息是隻能在4.4版本中實作。我總結了一下,隻需要以下五步就可以改變狀态欄顔色

第一步:導入支援包到工程

說明:這個支援包是我從github上的這個開源項目上托下來的,就是一個java檔案(SystemBarTintManager.java),在demo中大家自己下吧。友善我們自己修改。

第二步:修改主題檔案

首先你需要在你工程的res目錄下建立個Values-V19的包,然後再建個styles.xml,如下所示:

<resources>
    <!--
        Base application theme for API 19+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 19+ devices.
    -->
    <style name="ActionBarTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowTranslucentNavigation" >true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <!-- toolbar(actionbar)顔色 -->
        <item name="colorPrimary">#673AB7</item>
        <!-- 狀态欄顔色 -->
        <item name="colorPrimaryDark">#512DA8</item>
    </style>
</resources>
           

目錄結構如圖所示

Android 五步修改狀态欄顔色

這個樣式檔案的目的在于當系統版本大于19時,即安卓4.4,就會首先使用這裡面的主題樣式。

說明下,colorPrimary 是toolbar的顔色,toolbar在上面的那篇部落格中有詳細的介紹,這裡就不在介紹了。colorPrimaryDark是狀态欄的顔色。顔色的選取可以參考這個網站http://www.materialpalette.com/purple/orange。android:windowTranslucentNavigation,android:windowTranslucentStatus 這兩個屬性是必須有的,也可以在代碼中設定。樣式要使用NoActionBar的,這裡我們是用toolbar來取代actionbar。

第三步:清單檔案中應用主題

<activity android:name="MyActivity"
       android:theme="@style/ActionBarTheme"
       android:label="@string/app_name">
           

注意:Demo中使用了toolbar,是以Activity的樣式必須繼承于Theme.AppCompat,Activity也必須繼承自 ActionBarActivity,不然會報錯的。這裡最好的方式是在application節點下配置預設的樣式,這樣配置一次就可以了。

第四步:修改布局檔案

首先我們把toolbar單獨建立出來,這樣友善複用。如下在layout中建立toobar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
           

接着将toobar添加到我們的布局檔案中。如下main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true">

   <include layout="@layout/toolbar"></include>

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MyActivity"/>

</LinearLayout>
           

注意:android:fitsSystemWindows,如果置為true時,作用是空出狀态欄的位置,以免我們的的toolbar直接頂到螢幕的頂部。

第五步:修改代碼

在onCreate中添加以下代碼

//設定狀态欄的顔色,當版本大于4.4時起作用
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        //此處可以重新指定狀态欄顔色
        tintManager.setStatusBarTintResource(R.color.primary_dark);
    }
                

添加對toobar的支援

mToolbar = (Toolbar) findViewById(R.id.toolbar);
    // 标題的文字需在setSupportActionBar之前,不然會無效
    mToolbar.setLogo(R.drawable.ic_launcher);
    mToolbar.setTitle("主标題");
    setSupportActionBar(mToolbar);
           

最終效果對比如下

Android 五步修改狀态欄顔色

Demo源代碼下載下傳連結:http://pan.baidu.com/s/1gdvNFvt 密碼:uchi

轉載:http://www.codeweblog.com/android-%E4%BA%94%E6%AD%A5%E4%BF%AE%E6%94%B9%E7%8A%B6%E6%80%81%E6%A0%8F%E9%A2%9C%E8%89%B2/