Android system changes the status bar font color

  • 2020-12-13 19:06:09
  • OfStack

With the development of The Times, Android's status bar is no longer black. After Android4.4, we can change the color of the status bar or make our own View extend below the status bar. We can do more customization, but sometimes we use light colors like white, because the text above the status bar is white, so the text above the status bar cannot be seen clearly. Therefore, this article provides 1 solution, can be MIUI6+,Flyme4+, ES6en6.0 + support toggle status bar text color 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 solution provided by Xiaomi is mainly MIUI with built-in mode that can modify the status bar, supporting Dark and Light.
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;
}

Similarly to miui
Modify Android6. 0 +
Android 6.0 started with official support from Google, with android:windowLightStatusBar configured in the style attribute
When setting to true, when the background color of statusbar is light, the text color of statusbar will turn gray, and the same is true for 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>

Above is the Android system to change the status bar font color of the relevant code, I hope to help you learn.


Related articles: