public class StatusBarCompat {
private static final int INVALID_VAL = -1;
private static final int COLOR_DEFAULT = Color.parseColor("#20000000");
private static int MIUI = 942664;
private static int FLYME = 63498;
private static int ANDROID_M = 7482;
public static final String SYS_EMUI = "sys_emui";
public static final String SYS_MIUI = "sys_miui";
public static final String SYS_FLYME = "sys_flyme";
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 final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
private static final String KEY_EMUI_VERSION = "ro.build.version.emui";
private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void compat(Activity activity, int statusColor) {
//当前手机版本为5.0及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (statusColor != INVALID_VAL) {
activity.getWindow().setStatusBarColor(statusColor);
}
return;
}
//当前手机版本为4.4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
int color = COLOR_DEFAULT;
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (statusColor != INVALID_VAL) {
color = statusColor;
}
View statusBarView = new View(activity);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(activity));
statusBarView.setBackgroundColor(color);
contentView.addView(statusBarView, lp);
}
}
public static void compat(Activity activity) {
compat(activity, INVALID_VAL);
}
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
/**
* 修改状态栏为全透明,并且不影响导航栏颜色,0919亲测好使
*
* @param activity
*/
@TargetApi(19)
public static void transparencyBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
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);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.BLACK);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
if (StatusBarCompat.getSystem().equals(StatusBarCompat.SYS_MIUI)) {
Properties prop = new Properties();
if (prop.getProperty(KEY_MIUI_VERSION_NAME, "").equals("V6")) {//判断是否是V6
// StatusBarCompat.compat(activity, Color.parseColor("#000000"));
Window window = activity.getWindow();
Class clazz = window.getClass();
try {
int tranceFlag = 0;
int darkModeFlag = 0;
@SuppressLint("PrivateApi")
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_TRANSPARENT");
tranceFlag = field.getInt(layoutParams);
field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); //只需要状态栏透明
extraFlagField.invoke(window, tranceFlag, tranceFlag);// 或 //状态栏透明且黑色字体
extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag); //清除黑色字体
extraFlagField.invoke(window, 0, darkModeFlag);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} else {
StatusBarCompat.compat(activity, Color.parseColor("#000000"));
}
} else if (StatusBarCompat.getSystem().equals(StatusBarCompat.SYS_FLYME)) {
StatusBarCompat.compat(activity, Color.parseColor("#000000"));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//判断如果是6.0及其以上,才将状态栏字体和图标颜色变为黑色
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
StatusBarCompat.compat(activity, Color.parseColor("#000000"));
}
}
/**
* 修改状态栏为全透明,并且不影响导航栏颜色,0919亲测好使
*
* @param activity
*/
@TargetApi(19)
public static void transparencyBar(Activity activity, int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
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);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.BLACK);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
if (StatusBarCompat.getSystem().equals(StatusBarCompat.SYS_MIUI)) {
Properties prop = new Properties();
if (prop.getProperty(KEY_MIUI_VERSION_NAME, "").equals("V6")) {//判断是否是V6
// StatusBarCompat.compat(activity, Color.parseColor("#000000"));
Window window = activity.getWindow();
Class clazz = window.getClass();
try {
int tranceFlag = 0;
int darkModeFlag = 0;
@SuppressLint("PrivateApi")
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_TRANSPARENT");
tranceFlag = field.getInt(layoutParams);
field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); //只需要状态栏透明
extraFlagField.invoke(window, tranceFlag, tranceFlag);// 或 //状态栏透明且黑色字体
extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag); //清除黑色字体
extraFlagField.invoke(window, 0, darkModeFlag);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} else {
StatusBarCompat.compat(activity, Color.parseColor("#000000"));
}
} else if (StatusBarCompat.getSystem().equals(StatusBarCompat.SYS_FLYME)) {
StatusBarCompat.compat(activity, Color.parseColor("#000000"));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//判断如果是6.0及其以上,才将状态栏字体和图标颜色变为黑色
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
StatusBarCompat.compat(activity, Color.parseColor("#000000"));
}
}
public static String getSystem() {
String SYS = "";
try {
Properties prop = new Properties();
prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
if (prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null) {
SYS = SYS_MIUI;//小米
} else if (prop.getProperty(KEY_EMUI_API_LEVEL, null) != null
|| prop.getProperty(KEY_EMUI_VERSION, null) != null
|| prop.getProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, null) != null) {
SYS = SYS_EMUI;//华为
} else if (getMeizuFlymeOSFlag().toLowerCase().contains("flyme")) {
SYS = SYS_FLYME;//魅族
}
} catch (IOException e) {
e.printStackTrace();
return SYS;
}
return SYS;
}
public static String getMeizuFlymeOSFlag() {
return getSystemProperty("ro.build.display.id", "");
}
private static String getSystemProperty(String key, String defaultValue) {
try {
Class<?> clz = Class.forName("android.os.SystemProperties");
Method get = clz.getMethod("get", String.class, String.class);
return (String) get.invoke(clz, key, defaultValue);
} catch (Exception e) {
}
return defaultValue;
}
}
使用:
StatusBarCompat.transparencyBar(this);
StatusBarCompat.compat(this, Color.parseColor("#a5b4bb"));