赞
踩
应产品需求:需要将标题栏和状态栏设置成白色,状态栏字体颜色为黑色,如下图
这种情况可以想到的是沉浸式状态栏,可以按照以下图来设置颜色,或者查看我转载的另一个博客
http://blog.csdn.net/lf0814/article/details/69389219
即设置colorPrimaryDark的颜色为白色即可,但是这样的话标题栏的字体就看不见,然后查了好多资料,得出的结论是只有在Andorid6.0以上的才可以修改状态栏。鉴于这种情况,就给产品商量,将6.0以下的状态栏修改为黑色底,字体为系统原来颜色。
以下为适配的代码,在主基类中写即可
//状态栏颜色字体(白底黑字)修改 Android6.0+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(android.R.color.white));
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
//状态栏颜色字体(白底黑字)修改 MIUI6+
setMiuiStatusBarDarkMode(this,true);
//状态栏颜色字体(白底黑字)修改 Flyme4+
//setMeizuStatusBarDarkIcon(this,true);
适配MIUI
/**
* 状态栏颜色字体(白底黑字)修改 MIUI6+
* @param activity
* @param darkmode
* @return
*/
public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
Class<? extends Window> clazz = activity.getWindow().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);
extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
适配Flyme
/**
* 状态栏颜色字体(白底黑字)修改 Flyme4+
* @param activity
* @param dark
* @return
*/
public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
boolean result = false;
if (activity != null) {
try {
WindowManager.LayoutParams lp = activity.getWindow().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);
activity.getWindow().setAttributes(lp);
result = true;
} catch (Exception e) {
}
}
return result;
}
最后这里注释掉了setMeizuStatusBarDarkIcon(this,true); 只添加适配MIUI的后,魅族手机上也能满足需求,这个看情况吧,我只测试了一款魅族手机。如有写的不对的地方,请大家及时提出,谢谢。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。