Android狀態列圖示和字型如何變成深色
阿新 • • 發佈:2018-12-20
最近做了一個頁面,做好後卻發現狀態列的圖示字型什麼的都沒有了,很鬱悶,查了一下才知道安卓狀態列預設是白色的,所以把他的背景設定為與白色相近的顏色自然就看不見。 怎麼變深色呢,查了好多,終於成功了,但貌似安卓6.0以上才可以,我在安卓5.0的手機上試了不變色。 下面是效果圖。 具體程式碼實現如下
//首先建立一個類StatusBarUtil public class StatusBarUtil { /** * 設定狀態列黑色字型圖示, * 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android * * @return 1:MIUUI 2:Flyme 3:android6.0 */ public static int getStatusBarLightMode(Window window) { int result = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (MIUISetStatusBarLightMode(window, true)) { result = 1; } else if (FlymeSetStatusBarLightMode(window, true)) { result = 2; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); result = 3; } else {//5.0 } } return result; } /** * 已知系統型別時,設定狀態列黑色字型圖示。 * 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android */ public static void setStatusBarLightMode(Window window) { int type = getStatusBarLightMode(window); if (type == 1) { MIUISetStatusBarLightMode(window, true); } else if (type == 2) { FlymeSetStatusBarLightMode(window, true); } else if (type == 3) { window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } else {//5.0 } } /** * 清除MIUI或flyme或6.0以上版本狀態列黑色字型 */ public static void StatusBarDarkMode(Window window) { int type = getStatusBarLightMode(window); if (type == 1) { MIUISetStatusBarLightMode(window, false); } else if (type == 2) { FlymeSetStatusBarLightMode(window, false); } else if (type == 3) { window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } } /** * 設定狀態列圖示為深色和魅族特定的文字風格 * 可以用來判斷是否為Flyme使用者 * * @param window 需要設定的視窗 * @param dark 是否把狀態列字型及圖示顏色設定為深色 * @return boolean 成功執行返回true */ public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) { 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 (dark) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); window.setAttributes(lp); result = true; } catch (Exception e) { } } return result; } /** * 設定狀態列字型圖示為深色,需要MIUIV6以上 * * @param window 需要設定的視窗 * @param dark 是否把狀態列字型及圖示顏色設定為深色 * @return boolean 成功執行返回true */ public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) { 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 (dark) { extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//狀態列透明且黑色字型 } else { extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字型 } result = true; } catch (Exception e) { } } return result; } }
然後一定要記得在需要變顏色的Activity的onCreate()方法中呼叫
StatusBarUtil.setStatusBarLightMode(getWindow());
//在onCreate()方法中呼叫
END