一、设置状态栏颜色
1.调用代码
if (this.getStatusBarTintResource() != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){//6.0及6.0以上用该方法可以改善半透明效果
getWindow().setStatusBarColor(getResources().getColor(getStatusBarTintResource())); }else {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); } SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(color);//在这里可以填写自己需要的颜色 } }
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window window = getWindow();
WindowManager.LayoutParams winParams = window.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
window.setAttributes(winParams);
}
关于SystemBarTintManager类可以依赖compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
2.设置fitsSystemWindows属性
在根布局下设置android:fitsSystemWindows="true",或者在代码中获取到根布局对象,调用setFitsSystemWindows(true);
这个比较常见就不添加效果图了。
二、设置状态栏透明
同样调用以上代码,设置状态栏的颜色为透明。在清单文件中不设置android:fitsSystemWindows="true",如果在布局文件中设置了这个属性,可以在代码中调用setFitsSystemWindows(false)。
状态栏透明效果图