Android system change status bar font color

With the development of the times, the status bar of Android is not black. After Android 4.4, we can modify the color of the status bar or let our own View extend below the status bar. We can do more customization, but sometimes we use a light color such as white, because the text on the status bar is white, so the text on the status bar cannot be seen clearly. Therefore, this article provides some solutions, which can be MIUI6+, Flyme4+, Android6.0+ support switching the text color of the status bar to dark color.

Modify 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);
        return true;
    } catch (Exception e) {
        e.printStackTrace ();
    }
    return false;
}

 The above is the official solution provided by Xiaomi, mainly for MIUI built-in mode that can modify the status bar, supporting both Dark and Light modes.

Modify Flyme

 

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;
}

 In the same way, use a similar way to miui

 

Modify Android6.0+ 

Since Android 6.0, Google has officially provided support. You can configure android:windowLightStatusBar in the style attribute.
When set to true, when the background color of the statusbar is light, the text color of the statusbar will become gray, and the same is true when it is false.

<style name="statusBarStyle" parent="@android:style/Theme.DeviceDefault.Light">
    <item name="android:statusBarColor">@color/status_bar_color</item>
    <item name="android:windowLightStatusBar">false</item>
</style>

 

So far, the market share of android 6.0 is still very small, while the domestic market share of MIUI and flyme is fairly good. Therefore, we can do our best to adapt more. If you have other weird tricks, please share and add them.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326744733&siteId=291194637