当你在用别人的app时,发现别人的app状态栏和app的toolbar是一个颜色,在看一下自己的app发现我操 怎么是黑色的状态栏 这时的你肯定不爽,也想来一发,没关系、下面我们一起来一发。
首先你的设置app的主题、一般设置成noactionbar这样方便添加自定义的toolbar。
<style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/material_blue_500</item>
<item name="colorPrimaryDark">@color/material_blue_700</item>
<item name="colorAccent">@color/material_green_A200</item>
</style>
这里的colorPrimaryDark在Android 5.0的时候是可以直接设置成状态栏颜色的、但是4.4就没有这么先进了、下面给出4.4的操作。
面对Android 4.4时在res目录下面建立一个values-v19的目录表示。这里面的东东只支持4.4以上的api,然后再自己的activity里面创建自己的toolbar
<LinearLayout 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:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true" >
</android.support.v7.widget.Toolbar>
<Button
android:id="@+id/id_testBt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮一" />
</LinearLayout>
注意这里的toolbar中的android:fitsSystemWindows=”true”属性设置至关重要如果不这样设置上面的toolbar的背景无法覆盖上面的状态栏,但是如果toolbar的宽度你不设置成wrap_content 你就会发现此时的toolbar变矮了。
但是在5.0上的版本有可能出现,状态栏和下面的toolbar的颜色不太一样,这个时候你只能自己出手强制吧颜色设置成一样,推荐一个git开源库SystemBarTintManager这是一个强大的开源库,你可以随心所欲的操作状态栏的颜色,下面来点源码
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.statusbar_bg);//通知栏所需颜色
好,就这些了