Android状态栏黑色字体

a 夏天 提交于 2020-04-14 01:41:54

【今日推荐】:为什么一到面试就懵逼!>>>

###前言

由于公司项目的欢迎页是白色的,,修改状态栏颜色后,导致状态栏的白色字体完全被覆盖了,联想到之前在QQ、UC等一些app上都见到过状态栏的字体是深色的,想着,,必定有解决的方案。于是,有了本篇blog。

###参考 下面是我在网上找到的两篇文章

1.白底黑字!Android浅色状态栏黑色字体模式

2.Android-->沉浸式状态栏字体颜色的修改(只针对小米和魅族)

解决方案

源码传送门 ####MIUI

public class MIUIHelper implements IHelper {

    /**
     * 设置状态栏字体图标为深色,需要MIUI6以上
     *
     * @param isFontColorDark 是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    @Override
    public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
        Window window = activity.getWindow();
        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 (isFontColorDark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}

####flyme4+

public class FlymeHelper implements IHelper {

    /**
     * 设置状态栏图标为深色和魅族特定的文字风格
     * 可以用来判断是否为Flyme用户
     *
     * @param isFontColorDark 是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    @Override
    public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
        Window window = activity.getWindow();
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.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 (isFontColorDark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}

android6.0+

#####1.代码设置

public class AndroidMHelper implements IHelper {
    /**
     * @return if version is lager than M
     */
    @Override
    public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (isFontColorDark) {
                // 沉浸式
                //                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                //非沉浸式
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                //非沉浸式
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
            return true;
        }
        return false;
    }

}


#####2.style属性设置

<!--直接生效,状态栏文字颜色变成黑色,非沉浸式-->
<item name="android:windowLightStatusBar">true</item>

####思路1

st=>start
condMIUI=>condition: set MIUI6 success?
condFlyme=>condition: set flyme4+ success?
cond6=>condition: set 6.0+ success?
e=>end

st->condMIUI
condMIUI(no)->condFlyme
condFlyme(no)->cond6
cond6(no)->e
cond6(yes)->e
condMIUI(yes)->e
condFlyme(yes)->e

####思路2

st=>start
cond6=>condition: is 6.0+ ?
condFlyme=>condition: is flyme4+ ?
condMIUI=>condition: is MIUI6+ ?
e=>end

st->cond6
cond6(no)->condMIUI
condMIUI(no)->condFlyme
condFlyme(no)->6
cond6(yes)->e
condMIUI(yes)->e
condFlyme(yes)->e

###思路实现

public class Helper {
    @IntDef({
            OTHER,
            MIUI,
            FLYME,
            ANDROID_M
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface SystemType {

    }

    public static final int OTHER = -1;
    public static final int MIUI = 1;
    public static final int FLYME = 2;
    public static final int ANDROID_M = 3;

    /**
     * 设置状态栏黑色字体图标,
     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
     *
     * @return 1:MIUI 2:Flyme 3:android6.0
     */
    public static int statusBarLightMode(Activity activity) {
        @SystemType int result = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (new MIUIHelper().setStatusBarLightMode(activity, true)) {
                result = MIUI;
            } else if (new FlymeHelper().setStatusBarLightMode(activity, true)) {
                result = FLYME;
            } else if (new AndroidMHelper().setStatusBarLightMode(activity, true)) {
                result = ANDROID_M;
            }
        }
        return result;
    }

    /**
     * 已知系统类型时,设置状态栏黑色字体图标。
     * 适配4.4以上版本MIUI6、Flyme和6.0以上版本其他Android
     *
     * @param type 1:MIUI 2:Flyme 3:android6.0
     */
    public static void statusBarLightMode(Activity activity, @SystemType int type) {
        statusBarMode(activity, type, true);

    }

    /**
     * 清除MIUI或flyme或6.0以上版本状态栏黑色字体
     */
    public static void statusBarDarkMode(Activity activity, @SystemType int type) {
        statusBarMode(activity, type, false);
    }

    private static void statusBarMode(Activity activity, @SystemType int type, boolean isFontColorDark) {
        if (type == MIUI) {
            new MIUIHelper().setStatusBarLightMode(activity, isFontColorDark);
        } else if (type == FLYME) {
            new FlymeHelper().setStatusBarLightMode(activity, isFontColorDark);
        } else if (type == ANDROID_M) {
            new AndroidMHelper().setStatusBarLightMode(activity, isFontColorDark);
        }
    }

}

结论

本方案适配一下系统

  • android6.0+
  • MIUI6
  • flyme4+

据说:适配浅色状态栏深色字体的时候发现底层版本为Android6.0.1的MIUI7.1系统不支持View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR设置,还是得用MIUI自己的深色字体方法。所以,这里先适配MIUI跟flyme,再适配6.0,当然了,如果使用可以直接获取系统名,根据字符串判断,也可以先6.0在MIUI,但是这个不靠谱。还不如直接在6的系统上统一全配置上


说了这么多废话,,下面进入正题

福利

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!