###前言php
因为公司项目的欢迎页是白色的,,修改状态栏颜色后,致使状态栏的白色字体彻底被覆盖了,联想到以前在QQ、UC等一些app上都见到过状态栏的字体是深色的,想着,,一定有解决的方案。因而,有了本篇blog。html
###参考 下面是我在网上找到的两篇文章android
源码传送门 ####MIUIgit
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+github
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; } }
#####1.代码设置app
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属性设置ide
<!--直接生效,状态栏文字颜色变成黑色,非沉浸式--> <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
####思路2ui
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
###思路实现.net
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以上版本MIUI六、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的系统上统一全配置上
说了这么多废话,,下面进入正题