Android transparent status bar and setting status bar font color

Posted by ktsirig on Sat, 28 Dec 2019 20:40:47 +0100

Reference link https://blog.csdn.net/AmStrong_/article/details/78864613

Immersive status bar research is to extend the layout content to the status bar, so that the status bar can be covered or hidden on the layout. First of all, to modify the android version of the status bar at least above 4.4, and in 4.4, it is not allowed to make the status bar transparent, only to achieve a translucent shadow background, while in 5.x version, it is possible to modify the background color but not the font color, only above 6.0 can be modified at will. But in Meizu and Xiaomi's third-party ROM s above version 4.4, mobile phones provide modified interfaces. So the specific modification code is as follows:

1, Transparent status bar, height 0

	public static void setTranslucentStatus(Activity context, boolean flag) {
		//Determine whether the current device version number is more than 4.4. If so, call setTranslucentStatus to make the status bar transparent
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Window win = context.getWindow();
			WindowManager.LayoutParams winParams = win.getAttributes();
			final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
			if (flag) {
				winParams.flags |= bits;
			} else {
				winParams.flags &= ~bits;
			}
			win.setAttributes(winParams);
		}
	}

2, Change the font color in the status bar, white or black

	/**
	 * Status bar light color mode, setting status bar black text, icon,
	 * Adapts to MIUIV, Flyme and other Android versions above 4.4 and 6.0
	 * @return 1:MIUUI 2:Flyme 3:android6.0
	 */
	public static int setStatusBarLightMode(Activity activity,boolean isFontDark) {
		int result = 0;
		try{
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
				if (MIUISetStatusBarLightMode(activity, isFontDark)) {
					result = MIUI;
				} else if (FLYMESetStatusBarLightMode(activity.getWindow(), isFontDark)) {
					result = FLYME;
				} else 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);
					result = ANDROID_M;
				}else{
					setTranslucentStatus(activity,true);
				}
			}
		}catch(Exception e){
			MLog.logi("setStatusBarLightMode IS ERROR!!!");
		}
		return result;
	}

	/**
	 * When the system type is known, set the black text and icon in the status bar.
	 * Adapts to MIUIV, Flyme and other Android versions above 4.4 and 6.0
	 * @param type 1:MIUUI 2:Flyme 3:android6.0
	 */
	public static void setStatusBarLightMode(Activity activity, int type) {
		if (type == MIUI) {
			MIUISetStatusBarLightMode(activity, true);
		} else if (type == FLYME) {
			FLYMESetStatusBarLightMode(activity.getWindow(), true);
		} else if (type == ANDROID_M) {
			activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
		}
	}

	/**
	 * Set status bar icons to dark and Meizu specific text styles
	 * It can be used to determine whether it is a Flyme user
	 * @param window Windows to set
	 * @param dark   Set the status bar text and icon color to dark
	 * @return boolean Successful execution returns true
	 */
	private 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;
	}

	/**
	 * Need MIUIV6 or above
	 * @param dark Set the status bar text and icon color to dark
	 * @return boolean Successful execution returns true
	 */
	private static boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) {
		boolean result = false;
		Window window = activity.getWindow();
		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);//Status bar transparent and black font
				} else {
					extraFlagField.invoke(window, 0, darkModeFlag);//Clear black font
				}
				result = true;
				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //Development version 7.7.13 and later uses system API, the old method is invalid but will not report errors, so both methods should be added
					if (dark) {
						activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
					} else {
						activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

 

Topics: Flyme Android MIUI Mobile