###前言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的系統上統一全配置上
說了這麼多廢話,,下面進入正題