protected void setStatusBar(int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上 View decorView = getWindow().getDecorView(); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow().setStatusBarColor(color); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0 WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes(); localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags); } if (color == Color.WHITE) { getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); if (isFlyme()) { CommonUtils.setMeizuStatusBarDarkIcon(this, true); } else if (isMIUI()) { CommonUtils.setMiuiStatusBarDarkMode(this, true); } else if (Build.MANUFACTURER.equalsIgnoreCase("OPPO")) { CommonUtils.setOPPOStatusTextColor(true, this); } else { } } else { getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } }
/** * 判断手机是否是魅族 * * @return */ private static boolean isFlyme() { try { final Method method = Build.class.getMethod("hasSmartBar"); return method != null; } catch (final Exception e) { return false; } } /** * 判断手机是否是小米 * * @return */ private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code"; private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name"; private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage"; private static boolean isMIUI() { Properties prop = new Properties(); boolean isMIUI; try { prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop"))); } catch (IOException e) { e.printStackTrace(); return false; } isMIUI = prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null; return isMIUI; }
结合沉浸式状态 https://blog.csdn.net/qq_22060403/article/details/80665650