Android ToolBar 解析与应用(三)toolbar颜色设置

        如下图所示,能够设置颜色的部分有4个。分别是:状态栏,app bar,navigationbarcolor,窗体背景色.

 

状态栏透明色:

        1,在style主题样式中添加 android:windowTranslucentStatus 属性;并吧该style设置为activity的theme。

  1. <resources xmlns:tools="http://schemas.android.com/tools">
  2.    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  3.        <item name="android:windowTranslucentStatus" tools:targetApi="19">true</item>
  4.    </style>
  5. </resources>

        2,在layout中设置  android:fitsSystemWindows 属性,如下。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:layout_width="match_parent"
  4.    android:layout_height="match_parent"
  5.    android:fitsSystemWindows="true"
  6.    android:orientation="vertical">
  7. <android.support.v7.widget.Toolbar
  8.        android:id="@+id/id_toolbar"
  9.        android:layout_width="match_parent"
  10.        android:layout_height="wrap_content"
  11.        android:background="#009688"
  12.        android:minHeight="?attr/actionBarSize"/>
  13. </LinearLayout>

如此这般,状态栏颜色从原来的黑色,变为透明色了

 

app bar 背景色设置:

        如上段代码中的android:background="#009688",即可实现toolbar的背景色设置了。很简单吧

 

navigationbarcolor背景色设置:

        navigationbarcolor是5.0以上系统才会有的,所以相关属性的设置需要在文件(res/values-v21/styles.xml)中。

 

  1. <resources>>
  2.    <style name="AppTheme" parent="AppBaseTheme">
  3.        <item name="android:navigationBarColor">#009688</item>
  4.    </style>
  5. </resources>

        是不是也很简单?

 

窗体背景色设置:

        窗体背景色的设置就更加简单了,只需要在style.xml文件的如下设置,并吧该style设置为activity的theme即可。

 

  1. <resources xmlns:tools="http://schemas.android.com/tools">
  2. <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  3.    <item name="android:windowBackground">#00aaff</item>
  4. </style>
  5. </resources>
 



posted on 2015-12-13 13:45  张鹏飞_Flyko  阅读(1509)  评论(0)    收藏  举报

导航