public class MyStatusBarUtil {
/**
* 设置状态栏颜色
*
* @param activity activity
* @param colorRes 颜色res
*/
public static void initSystemBar(Activity activity,int colorRes) {
if (!MyTelephonyManager.isReadboyTablet()) {
setTranslucentStatus(activity,true);
SystemBarTintManager tintManager =new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
// 使用颜色资源
tintManager.setStatusBarTintResource(colorRes);
}
}
/**
* 全透状态栏
*/
public static void setStatusBarFullTransparent(Activity activity) {
if (Build.VERSION.SDK_INT >=21) {// 21表示5.0
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}else if (Build.VERSION.SDK_INT >=19) {// 19表示4.4
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 虚拟键盘也透明
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 状态栏白底黑字
*/
public static void setDarkStatusWhite(Activity activity,boolean bDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = activity.getWindow().getDecorView();
//这里对应的是状态栏的颜色,就是style中colorPrimaryDark的颜色
activity.getWindow().setStatusBarColor(Color.WHITE);
int vis = decorView.getSystemUiVisibility();
if (bDark) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(vis);
}
}
/**
* 状态栏白底黑字
*/
public static void setDarkStatusTrans(Activity activity,boolean bDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = activity.getWindow().getDecorView();
//这里对应的是状态栏的颜色,就是style中colorPrimaryDark的颜色
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
int vis = decorView.getSystemUiVisibility();
if (bDark) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(vis);
}
}
/**
* 状态栏白底黑字
*/
public static void setDarkStatusGray(Activity activity,boolean bDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = activity.getWindow().getDecorView();
//这里对应的是状态栏的颜色,就是style中colorPrimaryDark的颜色
activity.getWindow().setStatusBarColor(Color.parseColor("#f5f5f5"));
int vis = decorView.getSystemUiVisibility();
if (bDark) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(vis);
}
}
/**
* 状态栏白底黑字
*/
public static void setTransparentStatusDark(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = activity.getWindow().getDecorView();
//这里对应的是状态栏的颜色,就是style中colorPrimaryDark的颜色
activity.getWindow().setStatusBarColor(Color.parseColor("#00000000"));
int vis = decorView.getSystemUiVisibility();
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
decorView.setSystemUiVisibility(vis);
}
}
/**
* 状态栏黑底白字
*/
public static void setDarkStatusBlack(Activity activity,boolean bDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = activity.getWindow().getDecorView();
//这里对应的是状态栏的颜色,就是style中colorPrimaryDark的颜色
activity.getWindow().setStatusBarColor(Color.parseColor("#000000"));
int vis = decorView.getSystemUiVisibility();
if (bDark) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(vis);
}
}
@TargetApi(19)
private static void setTranslucentStatus(Activity activity,boolean on) {
Window win = activity.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);
}
}