Android 5.0以後支持statusbar 更換顏色,6.0以後支持夜間模式
/**
* 設置狀態欄的顏色
*/
protected void setStatusBarColor(int color, boolean lightTheme) {
if (lightTheme) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.M &&
!BaseDeviceUtils.isXiaoMi()) {
StatusBarUtil.setStatusBarColor(this, color, 40);
} else {
StatusBarUtil.setStatusBarColor(this, color, 0);
setStatusBarTheme(false);
}
} else {
StatusBarUtil.setStatusBarColor(this, color, 0);
setStatusBarTheme(true);
}
}
public void setStatusBarTheme(boolean darkMode) {
//原生6.0以上有API支持黑條狀態欄圖標,但是國產各ROM經過定製,
// 有的需要特定的設置才能實現
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = getWindow().getDecorView();
int systemuiVisibility = decorView.getSystemUiVisibility();
if (darkMode) {
decorView.setSystemUiVisibility(systemuiVisibility & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
decorView.setSystemUiVisibility(systemuiVisibility | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
this.setupStatusBarForOtherSystem(darkMode);
}
protected int getCustomStatusBarColor() {
if (isLightTheme()) {
return getResources().getColor(R.color.status_bar_color_light);
}
return getResources().getColor(R.color.status_bar_color_dark);
}
protected void setupStatusBarForOtherSystem(boolean dark) {
StatusBarUtil.setupStatusBarForOtherSystem(this, !dark);
}
public class StatusBarUtil {
/**
* 計算狀態欄顏色
*
* @param color color值
* @param alpha alpha值
* @return 最終的狀態欄顏色
*/
private static int calculateStatusColor(@ColorInt int color, int alpha) {
if (alpha == 0) {
return color;
}
float a = 1 - alpha / 255f;
int red = color >> 16 & 0xff;
int green = color >> 8 & 0xff;
int blue = color & 0xff;
red = (int) (red * a + 0.5);
green = (int) (green * a + 0.5);
blue = (int) (blue * a + 0.5);
return 0xff << 24 | red << 16 | green << 8 | blue;
}
/**
* 設置狀態欄顏色
*
* @param activity 需要設置的activity
* @param color 狀態欄顏色值
* @param statusBarAlpha 狀態欄透明度
*/
public static void setStatusBarColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));
}
}
public static boolean setupStatusBarForOtherSystem(Activity activity, boolean setDarkMode) {
if (BaseDeviceUtils.isXiaoMi()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Class<? extends Window> windowClass = activity.getWindow().getClass();
try {
Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager.LayoutParams");
int darkmode = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE").getInt(layoutParams);
Method setExtraFlags = windowClass.getMethod("setExtraFlags", Integer.TYPE, Integer.TYPE);
int mode = setDarkMode ? darkmode : 0;
setExtraFlags.invoke(activity.getWindow(),mode,darkmode);
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
return false;
}
}
public static boolean isXiaoMi() {
return Build.MANUFACTURER.equalsIgnoreCase("xiaomi");
}
不同的廠商可能實現不同,實測魅族可以。
最好封裝到BaseActivity 裏面,在初始化(setContentView 之前)的時候去改變statusbar 的顏色或者模式。
Activity 的setContentView 會先創建 DecorView 在創建DecorView 的時候會 根據Theme 去加載不同的ViewParent(RootView)(所以Theme在setContentView 之後調用無效),然後add進DecorView.一般rootview 包含了 Title 和content 兩部分,setContentView就是將
傳進來的view 添加到content。