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.     
  9.     <include layout="@layout/toolbar"></include>
  10.     
  11.     <TextView
  12.     android:layout_width="fill_parent"
  13.     android:layout_height="wrap_content"
  14.     android:text="Hello World, MyActivity"/>
  15.     
  16. </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>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值