一:首先在BaseActivity添加:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView=getWindow().getDecorView();
int option=View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
decorView.setSystemUiVisibility(option);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
二:可以写到工具类里面调用,不建议直接写BaseActivity里面,可能会出现改变不了字体颜色
private static final int MIUI =1;
private static final int FLYME =2;
private static final int ANDROID_M =3;
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){
}
return result;
}
public static void setTranslucentStatus(Activity context, boolean flag) {
//判断当前设备版本号是否为4.4以上,如果是,则通过调用setTranslucentStatus让状态栏变透明
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);
}
}
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;
}
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);//状态栏透明且黑色字体
}else {
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
}
result =true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错,所以两个方式都要加上
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;
}
就这么多了>>>>>>>>>>>>