Android 从4.4开始就已经支持自定义通知栏的背景色了!
先来一个效果图
要实现这样的效果只需要简单的3步
1.在values/style.xml(关于系统适配后面提到)
<style name="customeTheme" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
values-v19/style.xml
<style name="customeTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
values-v21/style.xml
<style name="customeTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--去除ActionBar-->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!--设置标题栏-->
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--针对5.x以后的系统,要把颜色设置透明,否则会呈现出系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
2.在AndroidManifest.xml中对指定的Activity的theme进行设置例如:
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2"
android:theme="@style/customeTheme">
</activity>
3.在Activity的布局文件中设置android:fitsSystemWindows为true,然后设置你想要的background就可以了例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:fitsSystemWindows="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cml.ceshilail.Activity1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>