1. 程式人生 > >Android 五步修改狀態列顏色

Android 五步修改狀態列顏色



五步修改狀態列顏色


標籤(空格分隔): 狀態列顏色變化 Android開發

一直以來對安卓系統的狀態列都不大滿意,在4.4以前只能選擇隱藏或者顯示,而不能夠改變其顏色以適應我們自己APP的整體風格。在安卓5.0釋出以後,介面實在美到爆,狀態列的顏色也可以自定義了。於是乎我就有想法將這一特性引入到我自己的APP中。查了很多資料,為了讓5.0以前版本的系統享受到material design,google自然會推出相應的相容包:即appcompathttps://www.zybuluo.com v21,這個相容包裡面有很多有意思的東西,不是這篇文章的重點,以後再講。在官方文件https://chris.banes.me/2014/10/17/appcompat-v21/
中介紹了可以引用這個包,然後在樣式中配置如下主題樣式就可以達到我們的目的

  1. <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
  2.     <!-- Here we setting appcompat’s actionBarStyle -->
  3.     <item name="actionBarStyle">@style/MyActionBarStyle</item>
  4.     <!-- ...and here we setting appcompat’s color theming attrs -->
  5.     <item name="colorPrimary">@color/my_awesome_red</item>
  6.     <item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
  7.     <!-- The rest of your attributes -->
  8. </style>
複製程式碼

>colorPrimaryDark 就是用於設定狀態列顏色的。但坑爹的是我按照文件寫了一個demo後狀態列顏色就是不改變,一度以為是我demo寫的有問題。上stackoverflow查了很久後發現不僅僅是我才遇到這個問題,這個可能是個小bug。總之想要現在通過v21這個包來實現在5.0以前的版本狀態列顏色變化是不可能的了。於是乎我google了好久,好訊息是這個特性可以實現,壞訊息是隻能在4.4版本中實現。我總結了一下,只需要以下五步就可以改變狀態列顏色


第一步:匯入支援包到工程[^code]
   說明:這個支援包是我從github上的開源專案上脫下來的,就是一個java檔案,方便我們自己修改。

第二步:修改主題檔案
   首先你需要在你工程的res目錄下新建個Values-V19的包,然後再建個styles.xml,如下所示:

  1. <resources>
  2.     <!--
  3.         Base application theme for API 19+. This theme completely replaces
  4.         AppBaseTheme from BOTH res/values/styles.xml and
  5.         res/values-v11/styles.xml on API 19+ devices.
  6.     -->    
  7.     <style name="ActionBarTheme" parent="Theme.AppCompat.NoActionBar">
  8.         <item name="android:windowTranslucentNavigation" >true</item>
  9.         <item name="android:windowTranslucentStatus">true</item>
  10.         <!-- toolbar(actionbar)顏色 -->
  11.         <item name="colorPrimary">#673AB7</item>
  12.         <!-- 狀態列顏色 -->
  13.         <item name="colorPrimaryDark">#512DA8</item>
  14.     </style>
  15. </resources>
複製程式碼

這個樣式檔案的目的在於當系統版本大於19時,即安卓4.4,就會首先使用這裡面的主題樣式。
說明下,**colorPrimary** 是toolbar的顏色,toolbar在上面的那篇部落格中有詳細的介紹,這裡就不在介紹了。**colorPrimaryDark**是狀態列的顏色。顏色的選取可以參考這個網站http://www.materialpalette.com/purple/orange。**android:windowTranslucentNavigation**,**android:windowTranslucentStatus** 這兩個屬性是必須有的,也可以在程式碼中設定。樣式要使用NoActionBar的,這裡我們是用toolbar來取代actionbar。
第三步:清單檔案中應用主題
  1. <activity android:name="MyActivity"
  2.            android:theme="@style/ActionBarTheme"
  3.            android:label="@string/app_name">
複製程式碼
**注意**:Demo中使用了toolbar,所以Activity的樣式必須繼承於Theme.AppCompat,Activity也必須繼承自 ActionBarActivity,不然會報錯的。這裡最好的方式是在application節點下配置預設的樣式,這樣配置一次就可以了。

第四步:修改佈局檔案
首先我們把toolbar單獨創建出來,這樣方便複用。如下在layout中建立toobar.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v7.widget.Toolbar 
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:id="@+id/toolbar"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="wrap_content"
  7.     android:background="?attr/colorPrimary"
  8.     android:minHeight="?attr/actionBarSize">
  9. </android.support.v7.widget.Toolbar>
複製程式碼

接著將toobar新增到我們的佈局檔案中。如下main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:orientation="vertical"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent"
  7.     android:fitsSystemWindows="true">
  8.     <include layout="@layout/toolbar"></include>
  9.     <TextView
  10.     android:layout_width="fill_parent"
  11.     android:layout_height="wrap_content"
  12.     android:text="Hello World, MyActivity"/>
  13. </LinearLayout>
複製程式碼

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

第五步修改程式碼
在onCreate中新增一下程式碼


  1.     //設定狀態列的顏色,當版本大於4.4時起作用
  2.     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  3.         SystemBarTintManager tintManager = new SystemBarTintManager(this);
  4.         tintManager.setStatusBarTintEnabled(true);
  5.         //此處可以重新指定狀態列顏色
  6.         tintManager.setStatusBarTintResource(R.color.primary_dark);
  7.     }
複製程式碼

新增對toobar的支援
  1. <p>mToolbar = (Toolbar) findViewById(R.id.toolbar);
  2.     // 標題的文字需在setSupportActionBar之前,不然會無效
  3.     mToolbar.setLogo(R.drawable.ic_launcher);
  4.     mToolbar.setTitle("主標題");
  5.     setSupportActionBar(mToolbar);</p><p><font style="background-color: rgb(255, 255, 255);">
  6. </font></p>