自定义Statusbar颜色十分漂亮,比原本黑色的高端太多
在V7包中使用AppbarLayout和Toolbar可以实现漂亮的效果,用Android Studio新建一个工程就可以看到。(前提别新建一个空工程)
今天使用SystemBarTint来实现。
首先下载相应的jar包,下载地址
然后导入工程,具体导入就不多说了。
然后导入后看代码:
直接在SetContentView下添加一下内容
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);//通知栏所需颜色,自定义即可然后是setTranslucentStatus()方法。
@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); }
官方demo:
public class MatchActionBarActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_actionbar);
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);//通知栏所需颜色
}
@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);
}
}
漂亮的界面就出来了,效果图就不贴了,代码十分简单,自己运行一遍即可。
最后是一点小问题:
头部的内容会被遮挡一部分,解决方法:
在最外面的父布局中添加 android:fitsSystemWindows="true"即可。
好了,一个炫酷的StatusBar就出现了。