必須先設(shè)置狀態(tài)欄背景顏色再設(shè)置狀態(tài)欄背景的亮色和暗色標(biāo)記(安卓系統(tǒng)會根據(jù)狀態(tài)欄這個標(biāo)記顯示亮色或者暗色的圖標(biāo)),注意這些都必須要放在setContentView前面,先看一下效果:
代碼調(diào)用的順序
最后分享一下工具類
/**
* Created zmm
* Functions:? 設(shè)置狀態(tài)欄透明并改變狀態(tài)欄顏色為深色工具類
*/
public class StatusBarUtils {
/**
* 設(shè)置魅族手機狀態(tài)欄圖標(biāo)顏色風(fēng)格
* 可以用來判斷是否為Flyme用戶
*
? ? * @param window 需要設(shè)置的窗口
? ? * @param dark? 是否把狀態(tài)欄字體及圖標(biāo)顏色設(shè)置為深色
? ? * @return boolean 成功執(zhí)行返回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;
? ? }
/**
* 設(shè)置小米手機狀態(tài)欄字體圖標(biāo)顏色模式铲汪,需要MIUIV6以上
*
? ? * @param window 需要設(shè)置的窗口
? ? * @param dark? 是否把狀態(tài)欄字體及圖標(biāo)顏色設(shè)置為深色
? ? * @return boolean 成功執(zhí)行返回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) {//狀態(tài)欄透明且黑色字體
? ? ? ? ? ? ? ? ? ? extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
? ? ? ? ? ? ? ? }else {//清除黑色字體
? ? ? ? ? ? ? ? ? ? extraFlagField.invoke(window, 0, darkModeFlag);
? ? ? ? ? ? ? ? }
result =true;
? ? ? ? ? ? }catch (Exception e) {
}
}
return result;
? ? }
/**
* 在不知道手機系統(tǒng)的情況下嘗試設(shè)置狀態(tài)欄字體模式為深色
* 也可以根據(jù)此方法判斷手機系統(tǒng)類型
* 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
*
? ? * @param activity
? ? * @return 1:MIUUI 2:Flyme 3:android6.0 0:設(shè)置失敗
*/
? ? public static void statusBarLightMode(Activity activity) {
int result =0;
? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (MIUISetStatusBarLightMode(activity.getWindow(), true)) {
//result = 1;
? ? ? ? ? ? ? ? StatusBarLightMode(activity, 1);
? ? ? ? ? ? }else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {
//result = 2;
? ? ? ? ? ? ? ? StatusBarLightMode(activity, 2);
? ? ? ? ? ? }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 = 3;
? ? ? ? ? ? ? ? StatusBarLightMode(activity, 3);
? ? ? ? ? ? }
}
}
/**
* 已知系統(tǒng)類型時玻蝌,設(shè)置狀態(tài)欄字體圖標(biāo)為深色万俗。
* 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
*
? ? * @param activity
? ? * @param type? ? 1:MIUUI 2:Flyme 3:android6.0
*/
? ? public static void StatusBarLightMode(Activity activity, int type) {
if (type ==1) {
MIUISetStatusBarLightMode(activity.getWindow(), true);
? ? ? ? }else if (type ==2) {
FlymeSetStatusBarLightMode(activity.getWindow(), true);
? ? ? ? }else if (type ==3) {
Window window = activity.getWindow();
? ? ? ? ? ? window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
? ? ? ? }
}
/**
* 已知系統(tǒng)類型時漓骚,清除MIUI或flyme或6.0以上版本狀態(tài)欄字體深色模式
*
? ? * @param activity
? ? * @param type? ? 1:MIUUI 2:Flyme 3:android6.0
*/
? ? public static void StatusBarDarkMode(Activity activity, int type) {
if (type ==1) {
MIUISetStatusBarLightMode(activity.getWindow(), false);
? ? ? ? }else if (type ==2) {
FlymeSetStatusBarLightMode(activity.getWindow(), false);
? ? ? ? }else if (type ==3) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
? ? ? ? }
}
/**
* 狀態(tài)欄背景透明
? ? * @param activity
? ? */
? ? public static void StatusBarTransport(Activity activity) {
Window window = activity.getWindow();
? ? ? ? window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
? ? ? ? ? ? ? ? | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
? ? ? ? window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
? ? ? ? ? ? ? ? | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
? ? ? ? ? ? ? ? | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
? ? ? ? window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(Color.TRANSPARENT);
? ? ? ? ? ? window.setNavigationBarColor(Color.TRANSPARENT);
? ? ? ? }
}
}