首先感谢前人的努力探索:
http://www.jianshu.com/p/7f5a9969be53
http://blog.csdn.net/kezhenlu/article/details/51556160
http://blog.csdn.net/angcyo/article/details/49834739
http://www.open-open.com/lib/view/open1452308426605.html
4.4以上可以做状态栏的沉浸式,在沉浸式的颜色和时间wifi等颜色相同时,为了满足设计,就要改变wifi等状态栏字体的颜色,首先来说,这是一个系统级别的功能,并不是什么系统,什么型号的手机都支持..适配浅色状态栏深色字体的时候发现底层版本为Android6.0.1的MIUI7.1系统不支持View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR设置,还是得用MIUI自己的深色字体方法。
目前可以是MIUI6+,Flyme4+,Android6.0+支持切换状态栏的文字颜色为暗色。
小米手机
public class MIUIHelper implements IHelper {
/**
* 设置状态栏字体图标为深色,需要MIUI6以上
*
* @param isFontColorDark 是否把状态栏字体及图标颜色设置为深色
* @return boolean 成功执行返回true
*/
@Override
public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
Window window = activity.getWindow();
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 (isFontColorDark) {
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
} else {
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
}
result = true;
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
魅族手机4+:
public class FlymeHelper implements IHelper {
/**
* 设置状态栏图标为深色和魅族特定的文字风格
* 可以用来判断是否为Flyme用户
*
* @param isFontColorDark 是否把状态栏字体及图标颜色设置为深色
* @return boolean 成功执行返回true
*/
@Override
public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
Window window = activity.getWindow();
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 (isFontColorDark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(lp, value);
window.setAttributes(lp);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
android6.0+有两种方法:
1,代码
public class AndroidMHelper implements IHelper {
/**
* @return if version is lager than M
*/
@Override
public boolean setStatusBarLightMode(Activity activity, boolean isFontColorDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (isFontColorDark) {
// 沉浸式
// activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//非沉浸式
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
//非沉浸式
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
return true;
}
return false;
}
}
2,xml形式
<!--直接生效,状态栏文字颜色变成黑色,非沉浸式-->
<item name="android:windowLightStatusBar">true</item>
统一方案:
<style name="statusBarStyle" parent="@android:style/Theme.DeviceDefault.Light">
<item name="android:statusBarColor">@color/status_bar_color</item>
<item name="android:windowLightStatusBar">false</item>
</style>
统一上面的方案,就有一下的适配代码:
暂时不贴了,其实都差不多,做 if --else --- 就行了.
OK.