public static final int MIN_API = 17;
//沉浸式
public static void immersive(Activity activity) {
immersive(activity, DEFAULT_COLOR, DEFAULT_ALPHA);
}
public static void immersive(Activity activity, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
immersive(activity.getWindow(), color, alpha);
}
public static void immersive(Activity activity, int color) {
immersive(activity.getWindow(), color, 1f);
}
public static void immersive(Window window) {
immersive(window, DEFAULT_COLOR, DEFAULT_ALPHA);
}
public static void immersive(Window window, int color) {
immersive(window, color, 1f);
}
@SuppressLint(“ObsoleteSdkInt”)
public static void immersive(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (Build.VERSION.SDK_INT >= 21) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(mixtureColor(color, alpha));
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
} else if (Build.VERSION.SDK_INT >= 19) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
} else if (Build.VERSION.SDK_INT >= MIN_API && Build.VERSION.SDK_INT > 16) {
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
}
}
//
//
public static void darkMode(Activity activity, boolean dark) {
if (isFlyme4Later()) {
darkModeForFlyme4(activity.getWindow(), dark);
} else if (isMIUI6Later()) {
darkModeForMIUI6(activity.getWindow(), dark);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
darkModeForM(activity.getWindow(), dark);
}
}
/**
• 设置状态栏darkMode,字体颜色及icon变黑(目前支持MIUI6以上,Flyme4以上,Android M以上)
*/
public static void darkMode(Activity activity) {
darkMode(activity.getWindow(), DEFAULT_COLOR, DEFAULT_ALPHA);
}
public static void darkMode(Activity activity, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
darkMode(activity.getWindow(), color, alpha);
}
/**
• 设置状态栏darkMode,字体颜色及icon变黑(目前支持MIUI6以上,Flyme4以上,Android M以上)
*/
public static void darkMode(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (isFlyme4Later()) {
darkModeForFlyme4(window, true);
immersive(window, color, alpha);
} else if (isMIUI6Later()) {
darkModeForMIUI6(window, true);
immersive(window, color, alpha);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
darkModeForM(window, true);
immersive(window, color, alpha);
} else if (Build.VERSION.SDK_INT >= 19) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
} else {
immersive(window, color, alpha);
}
if (Build.VERSION.SDK_INT >= 21) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
setTranslucentView((ViewGroup) window.getDecorView(), color, alpha);
}
//------------------------->
/**
• android 6.0设置字体颜色
*/
@RequiresApi(Build.VERSION_CODES.M)
private static void darkModeForM(Window window, boolean dark) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
if (dark) {
systemUiVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
systemUiVisibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
window.getDecorView().setSystemUiVisibility(systemUiVisibility);
}
/**
• 设置Flyme4+的darkMode,darkMode时候字体颜色及icon变黑
• http://open-wiki.flyme.cn/index.php?title=Flyme%E7%B3%BB%E7%BB%9FAPI
*/
public static boolean darkModeForFlyme4(Window window, boolean dark) {
boolean result = false;
if (window != null) {
try {
WindowManager.LayoutParams e = window.getAttributes();
Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField(“MEIZU_FLAG_DARK_STATUS_BAR_ICON”);
Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField(“meizuFlags”);
darkFlag.setAccessible(true);
meizuFlags.setAccessible(true);
int bit = darkFlag.getInt(null);
int value = meizuFlags.getInt(e);
if (dark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(e, value);
window.setAttributes(e);
result = true;
} catch (Exception var8) {
Log.e(“StatusBar”, “darkIcon: failed”);
}
}
return result;
}
/**
• 设置MIUI6+的状态栏是否为darkMode,darkMode时候字体颜色及icon变黑
• http://dev.xiaomi.com/doc/p=4769/
*/
@SuppressLint(“PrivateApi”)
public static boolean darkModeForMIUI6(Window window, boolean darkmode) {
Class<? extends Window> clazz = window.getClass();
try {
int darkModeFlag = 0;
Class<?> layoutParams = Class.forName(“android.view.MiuiWindowManager$LayoutParams”);
Field field = layoutParams.getField(“EXTRA_FLAG_STATUS_BAR_DARK_MODE”);
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod(“setExtraFlags”, int.class, int.class);
extraFlagField.invoke(window, darkmode ? darkModeFlag : 0, darkModeFlag);
} catch (Exception e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
darkModeForM(window, darkmode);
}
return true;
}
/**
• 判断是否Flyme4以上
*/
public static boolean isFlyme4Later() {
return Build.FINGERPRINT.contains(“Flyme_OS_4”)
|| Build.VERSION.INCREMENTAL.contains(“Flyme_OS_4”)
|| Pattern.compile(“Flyme OS [4|5]”, Pattern.CASE_INSENSITIVE).matcher(Build.DISPLAY).find();
}
/**
• 判断是否为MIUI6以上
*/
@SuppressLint(“PrivateApi”)
public static boolean isMIUI6Later() {
try {
Class<?> clz = Class.forName(“android.os.SystemProperties”);
Method mtd = clz.getMethod(“get”, String.class);
String val = (String) mtd.invoke(null, “ro.miui.ui.version.name”);
assert val != null;
val = val.replaceAll(“[vV]”, “”);
int version = Integer.parseInt(val);
return version >= 6;
} catch (Exception e) {
return false;
}
}
//
/**
• 增加View的paddingTop,增加的值为状态栏高度
*/
public static void setPadding(Context context, View view) {
if (Build.VERSION.SDK_INT >= MIN_API) {
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context),
view.getPaddingRight(), view.getPaddingBottom());
}
}
/**
• 增加View的paddingTop,增加的值为状态栏高度 (智能判断,并设置高度)
*/