The following methods for changing the font of immersive navigation bar in Android 6.0 (only valid for some mobile phones)

Time:2021-7-30

This method is only valid for some phones below Android 6.0 (the color can be modified successfully). If it is invalid, the navigation bar is translucent and the font can be seen.

Android immersive navigation bar is very common, which can “integrate” the status bar with the title bar of the mobile phone and look more beautiful. However, if the title bar is white in the design drawing given by the UI, the font color of your mobile phone’s status bar is also white, which will lead to the invisibility of the navigation bar font, as shown in the figure:

In this case, a little friend said that MIUI and flyme provide open source code to set the font color of the status bar, just like the same “statusbarutil” class after we search “Android changes the font color of the title bar”. This class is really useful for Android 6.0 phones,

But what about the lower version? Let’s take a look at this class first:

package com.matai.sharecar.utils;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 *Author: www.on.2019/8/27
 *Class description:
 */

public class StatusBarUtil3 {

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

    /**
     *Set the status bar text color value to dark color
     *
     *Does @ param usedart use dark tones
     * @param activity
     */
    public static void setStatusTextColor(boolean useDart, Activity activity) {
        If (isflyme()) {// judge whether it is Meizu
            processFlyMe(useDart, activity);// Call flyme open source code
        }Else if (ismiui()) {// judge whether it is Xiaomi
            processMIUI(useDart, activity);// Call MIUI open source code
        }Else {// neither
            If (usedart) {// if dark tone is set
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }else{
                    //When the above conditions are not met, there is no processing!!!!!
                }
            }Else {// no dark tone is set
                activity.getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
    }

    /**
     *Judge whether the mobile phone is Meizu
     *
     * @return
     */
    public static boolean isFlyme() {
        try {
            // Invoke Build.hasSmartBar()
            final Method method = Build.class.getMethod("hasSmartBar");
            return method != null;
        } catch (final Exception e) {
            return false;
        }
    }

    /**
     *Change the font of Meizu's status bar to black and require flyme4 or above
     */
    private static void processFlyMe(boolean isLightStatusBar, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        try {
            Class> instance = Class.forName("android.view.WindowManager$LayoutParams");
            int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp);
            Field field = instance.getDeclaredField("meizuFlags");
            field.setAccessible(true);
            int origin = field.getInt(lp);
            if (isLightStatusBar) {
                field.set(lp, origin | value);
            } else {
                field.set(lp, (~value) & origin);
            }
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }

    /**
     *Judge whether the mobile phone is Xiaomi
     *
     * @return
     */
    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;
        }
    }

    /**
     *Change the font color of Xiaomi's status bar to black. If the lightstatusbar above miui6 is true, it means black font
     */
    private static void processMIUI(boolean lightStatusBar, Activity activity) {
        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        Class extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag;
            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(), lightStatusBar? darkModeFlag : 0, darkModeFlag);
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }

}

The code meaning has been described in detail in the comments. The usage method is in mainactivity:StatusBarUtil.setStatusTextColor(true,this);

The effect is shown in the figure:

 

However, it was not successfully modified on my test machine (vivo Android 5.1)… So I found out how to modify the color of the title bar in the funtouch OS System on the Internet and found this method:

 

/**
     *Set the font of oppo mobile phone status bar to black (some mobile phones below coloros3.0,6.0)
     *
     * @param lightStatusBar
     * @param activity
     */
    private static final int SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT = 0x00000010;

    public static void setOPPOStatusTextColor(boolean lightStatusBar, Activity activity) {
        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        int vis = window.getDecorView().getSystemUiVisibility();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (lightStatusBar) {
                vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            } else {
                vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            if (lightStatusBar) {
                vis |= SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT;
            } else {
                vis &= ~SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT;
            }
        }
        window.getDecorView().setSystemUiVisibility(vis);
    }

 

After the test, I found that the oppo mobile phone is valid, but it is still invalid on vivo. Later, I found that it is only suitable for some mobile phones under os6.0, but it can’t make the lower version mobile phones can’t see the title bar, so I thought of a simple and rough method:

/**
     *Set the status bar text color value to dark color
     *
     *Does @ param usedart use dark tones
     * @param activity
     */
    public static void setStatusTextColor(boolean useDart, Activity activity) {
        If (isflyme()) {// judge whether it is Meizu
            processFlyMe(useDart, activity);// Using flyme open source code
        }Else if (ismiui()) {// judge whether it is Xiaomi
            processMIUI(useDart, activity);// Open source code with MIUI
        }Else if (build.manufacturer.equalsignorecase ("oppo") {// judge whether it is oppo (some people may say why not build.manufacturer.equalsignorecase ("vivo"))? Is that why you don't show vivo( Answer: no)
            setOPPOStatusTextColor(useDart, activity);//
        }Else {// none of the three
            if (useDart) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }else{
                    //The point is to set it directly
                    activity.getWindow().getDecorView().setSystemUiVisibility(Color.parseColor("#000000"));
                }
            } else {
                activity.getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
    }

After that, my mobile phone runs like this: Although the title bar has color   But at least the words can be seen. I can only do this. If you have a better way, you must tell me!!!!

 

As a record   I hope I can help you.