1、设置顶部状态栏字体颜色为白色红底
public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
View decorView = context.getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(option);
//根据上面设置是否对状态栏单独设置颜色
if (useThemeStatusBarColor) {
context.getWindow().setStatusBarColor(Color.parseColor("#FF3E96"));
} else {
context.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
}
2、设置状态栏字体颜色为黑色
public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = context.getWindow().getDecorView();
context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
if (useThemeStatusBarColor) {
context.getWindow().setStatusBarColor(Color.parseColor("#FF3E96"));
context.getWindow().setNavigationBarColor(Color.parseColor("#FF3E96"));//40000000
} else {
context.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
}
3、设置虚拟按键栏背景颜色
public static void setNavigationBarColor(Activity activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().setNavigationBarColor(Color.parseColor("#FF3E96"));
}
}
4、设置虚拟按键栏和状态栏字体图标为黑色,白底
public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
//根据上面设置是否对状态栏单独设置颜色
if (useThemeStatusBarColor) {
context.getWindow().setStatusBarColor(Color.parseColor("#ffffffff"));
context.getWindow().setNavigationBarColor(Color.parseColor("#ffffffff"));//40000000
} else {
context.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
}
这些方法不一定适配所有手机,如果是DialogFragment就将activity改成Dialog即可。