StatusBar背景色和字体颜色设置

设置状态栏背景色

支持5.0以上,4.4只支持设置是否半透明

public static void setStatusBarBackgroundColor(Window window, int color) {
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(color);
}

设置状态栏字体颜色

支持6.0以上 - 只能设置白色与深色,6.0以上小米貌似遇到不支持的情况

public static boolean setStatusBarTextColor(Window window, boolean lightStatusBar) {
    // 设置状态栏字体颜色 白色与深色
    View decor = window.getDecorView();
    int ui = decor.getSystemUiVisibility();
    ui |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (lightStatusBar) {
            ui |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        } else {
            ui &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        }
    }
    decor.setSystemUiVisibility(ui);
}

针对小米和魅族-修改状态栏字体颜色

static class StatusBarModelFit {

        public static boolean setMiuiStatusBarDarkMode(Window window, boolean darkMode) {
            try {
                Class layoutParamsClazz = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParamsClazz.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                int darkModeFlag = field.getInt(null);
                Class windowClazz = window.getClass();
                Method setExtraFlagsMethod = windowClazz.getMethod("setExtraFlags", int.class, int.class);
                setExtraFlagsMethod.invoke(window, darkMode ? darkModeFlag : 0, darkModeFlag);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static boolean setMeizuStatusBarDarkIcon(Window window, boolean dark) {
            boolean result = false;
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Class layoutParamsClazz = WindowManager.LayoutParams.class;
                Field darkFlag = layoutParamsClazz.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = layoutParamsClazz.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);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    }

获取系统厂商

小米,魅族,华为


    static class SystemHelper {
        public static final String SYS_EMUI = "sys_emui";
        public static final String SYS_MIUI = "sys_miui";
        public static final String SYS_FLYME = "sys_flyme";
        public static final String SYS_DEFAULT = "sys_default";
        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";
        private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
        private static final String KEY_EMUI_VERSION = "ro.build.version.emui";
        private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.config.hw_systemversion";
        private static final String UNKNOWN = "unknown";

        private static String sCurSystem = "";

        public static String getSystem() {
            if (!TextUtils.isEmpty(sCurSystem)) {
                return sCurSystem;
            }
            String sys = SYS_DEFAULT;
            if (!UNKNOWN.equals(getProperty(KEY_MIUI_VERSION_CODE, UNKNOWN))
                    || !UNKNOWN.equals(getProperty(KEY_MIUI_VERSION_NAME, UNKNOWN))
                    || !UNKNOWN.equals(getProperty(KEY_MIUI_INTERNAL_STORAGE, UNKNOWN))) {
                sys = SYS_MIUI; // 小米
            } else if (getProperty("ro.build.display.id", UNKNOWN).toLowerCase().contains("flyme")) {
                sys = SYS_FLYME; // 魅族
            } else if (!UNKNOWN.equals(getProperty(KEY_EMUI_API_LEVEL, UNKNOWN))
                    || !UNKNOWN.equals(getProperty(KEY_EMUI_VERSION, UNKNOWN))
                    || !UNKNOWN.equals(getProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, UNKNOWN))) {
                sys = SYS_EMUI; // 华为
            }
            sCurSystem = sys;
            return sys;
        }

        public static String getProperty(String key, String defaultValue) {
            try {
                Class clz = Class.forName("android.os.SystemProperties");
                Method get = clz.getMethod("get", String.class, String.class);
                return (String) get.invoke(clz, key, defaultValue);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return defaultValue;
        }
    }

你可能感兴趣的:(StatusBar背景色和字体颜色设置)