我们在微信或者QQ 网易云上都有看到过状态栏上的颜色或者 背景是一致的。最近也用到这个需求,分享一下代码
代码很简单两步就足够了
第一步,在需要设置沉浸式效果的Activity中调用此方法,如果整个APP统一的话建议在baseActivity中调用,就不需要每个activity都重写了。
/**
* 设置状态栏颜色
* @param activity
*/
public static void setBarTopColor(Activity activity,int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //系统版本大于19
setTranslucentStatus(true,activity);
}
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(color); //设置标题栏颜色,此颜色在color中声明
}
第二步,在activity布局的根layout中添加一句代码, android:fitsSystemWindows="true"
例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
搞定了!就是这么简单。当然你的bar也要设置相同的颜色,上效果图.
以上