1.判断系统所用工具类
public class BuildProperties { private final Properties properties; private BuildProperties() throws IOException { properties = new Properties(); properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop"))); } public boolean containsKey(final Object key) { return properties.containsKey(key); } public boolean containsValue(final Object value) { return properties.containsValue(value); } public Set<Entry<Object, Object>> entrySet() { return properties.entrySet(); } public String getProperty(final String name) { return properties.getProperty(name); } public String getProperty(final String name, final String defaultValue) { return properties.getProperty(name, defaultValue); } public boolean isEmpty() { return properties.isEmpty(); } public Enumeration<Object> keys() { return properties.keys(); } public Set<Object> keySet() { return properties.keySet(); } public int size() { return properties.size(); } public Collection<Object> values() { return properties.values(); } public static BuildProperties newInstance() throws IOException { return new BuildProperties(); } }
2.判断系统是否为小米
public class MIUIUtils { private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code"; private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name"; private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage"; public static boolean isMIUI() { try { final BuildProperties prop = BuildProperties.newInstance(); return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null; } catch (final IOException e) { return false; } } }
3.
public class ChangeStatusBarTextColor { public static void changeColor(Activity activity, Boolean isDark) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if(MIUIUtils.isMIUI()) { //针对小米 Window window2 =activity.getWindow(); window2.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window2.setStatusBarColor(Color.parseColor("#0864A1")); window2.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window2.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); }else { //一般手机 Window window = activity.getWindow(); //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏 // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //设置状态栏颜色 window.setStatusBarColor(Color.parseColor("#0864A1")); // window.setBackgroundDrawableResource(R.drawable.nav_bg); activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } } } //对于小米miui public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) { Class<? extends Window> clazz = activity.getWindow().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(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag); if (darkmode) {//状态栏透明且黑色字体 extraFlagField.invoke(activity.getWindow(), darkModeFlag, darkModeFlag); } else {//清除黑色字体 extraFlagField.invoke(activity.getWindow(), 0, darkModeFlag); } return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 设置小米手机状态栏字体图标颜色模式,需要MIUIV6以上 * * @param window 需要设置的窗口 * @param dark 是否把状态栏字体及图标颜色设置为深色 * @return boolean 成功执行返回true */ public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) { boolean result = false; if (window != null) { Class 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); if (dark) {//状态栏透明且黑色字体 extraFlagField.invoke(window, darkModeFlag, darkModeFlag); } else {//清除黑色字体 extraFlagField.invoke(window, 0, darkModeFlag); } result = true; } catch (Exception e) { } } return result; } //对于魅族 public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) { boolean result = false; if (activity != null) { try { WindowManager.LayoutParams lp = activity.getWindow().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(lp); if (dark) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); activity.getWindow().setAttributes(lp); result = true; } catch (Exception e) { } } return result; } }
4.调用
ChangeStatusBarTextColor.changeColor(MyActivity.this,true);