Android 五步修改狀態列顏色
阿新 • • 發佈:2019-02-20
五步修改狀態列顏色
標籤(空格分隔): 狀態列顏色變化 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/
-
<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版本中實現。我總結了一下,只需要以下五步就可以改變狀態列顏色
第一步:匯入支援包到工程[^code]
說明:這個支援包是我從github上的開源專案上脫下來的,就是一個java檔案,方便我們自己修改。
第二步:修改主題檔案
首先你需要在你工程的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>
這個樣式檔案的目的在於當系統版本大於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的支援
-
<p>mToolbar = (Toolbar) findViewById(R.id.toolbar);
-
// 標題的文字需在setSupportActionBar之前,不然會無效
-
mToolbar.setLogo(R.drawable.ic_launcher);
-
mToolbar.setTitle("主標題");
-
setSupportActionBar(mToolbar);</p><p><font style="background-color: rgb(255, 255, 255);">
- </font></p>