android设置通知栏颜色

首先到github上下载SystemBarTint,将里面的SystemBarTintManager.java拷贝出来放到自己的项目中, 在actitvity的onCreate()方法中加入如下代码

代码如下:

1
2
3
4
5
6
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus( true );
}
SystemBarTintManager tintManager = new SystemBarTintManager( this );
tintManager.setStatusBarTintEnabled( true );
tintManager.setStatusBarTintResource(R.color.statusbar_bg); //通知栏所需颜色

下面是设置通知栏的状态

1
2
3
4
5
6
7
8
9
10
11
12
@TargetApi ( 19 )
private void setTranslucentStatus( boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}

这样就可以完美实现沉浸式通知栏的。

注意:

在需要透明状态栏的XML布局文件的Top-level中加上如下属性:

android:fitsSystemWindows="true"
否则会遮挡标题栏

猜你喜欢

转载自blog.csdn.net/u014172743/article/details/51513914
今日推荐