天天看点

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/