Android支持在API 19及以上使用沉浸式状态,但在MIUI V6下如果扩展的颜色比较浅,会导致状态栏的文字无法看清。

在Android4.4设备上支持沉浸式状态栏,只需要添加values-v19/styles.xml 下添加

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> <item name="android:windowTranslucentNavigation">false</item> <item name="android:windowTranslucentStatus">true</item> </style>

然后在可以扩展的控件添加属性android:fitsSystemWindows="true"
就阔以了。
但在MIUI V6下如果扩展的颜色比较浅,会导致状态栏的文字无法看清。在其他ROM上会有渐变的灰色区域。

MIUI提供了新的解决方案,在MIUI V6上状态栏支持灰黑色和白色两种字体颜色,开发者可以直接设置当前界面状态栏的文字颜色。

具体代码:

/** * 只支持MIUI V6 * @param context * @param type 0--只需要状态栏透明 1-状态栏透明且黑色字体 2-清除黑色字体 */    public static void setStatusBarTextColor(Activity context,int type){        if (!isMiUIV6()){            DebugLog.d("isMiUIV6:"+false);            return;        }        DebugLog.d("isMiUIV6:"+true);        Window window = context.getWindow();        Class clazz = window.getClass();        try {            int tranceFlag = 0;            int darkModeFlag = 0;            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);            if (type == 0){                extraFlagField.invoke(window, tranceFlag, tranceFlag);//只需要状态栏透明            }else if(type == 1){                extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag);//状态栏透明且黑色字体            }else {                extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体            }        }catch (Exception e){        }    }    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";    private static boolean isMiUIV6() {        try {            final BuildProperties prop = BuildProperties.newInstance();            String name = prop.getProperty(KEY_MIUI_VERSION_NAME, "");            if ("V6".equals(name)){                return  true;            }else {                return false;            }// return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null// || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null// || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;        } catch (final IOException e) {            return false;        }    }

BuildProperties.java

public class BuildProperties {    private final Properties properties;    private BuildProperties() throws IOException {        properties = new Properties();        properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));    }    public boolean containsKey(final Object key) {        return properties.containsKey(key);    }    public boolean containsValue(final Object value) {        return properties.containsValue(value);    }    public Set<java.util.Map.Entry<Object, Object>> entrySet() {        return properties.entrySet();    }    public String getProperty(final String name) {        return properties.getProperty(name);    }    public String getProperty(final String name, final String defaultValue) {        return properties.getProperty(name, defaultValue);    }    public boolean isEmpty() {        return properties.isEmpty();    }    public Enumeration<Object> keys() {        return properties.keys();    }    public Set<Object> keySet() {        return properties.keySet();    }    public int size() {        return properties.size();    }    public Collection<Object> values() {        return properties.values();    }    public static BuildProperties newInstance() throws IOException {        return new BuildProperties();    }}

更多相关文章

  1. 【译】Android位图颜色模式的问题
  2. android中颜色的定义
  3. android textview宽度固定的情况下字体大小自适应
  4. Android修改SwitchPreference的颜色
  5. android 各种颜色值 colors.xml
  6. Android碎碎念 -- 设置文字颜色
  7. Android TextView 个别文字字体颜色格式
  8. 输入键盘始终停留以及尺寸转换得到默认字体大小
  9. 点击listitem按下效果替换默认颜色

随机推荐

  1. Android学习感悟之消息机制
  2. android 通过Application类 共享全局数据
  3. Android(安卓)9.0 (P)
  4. 信息提醒之Notification,兼容全部SDK-更新
  5. android studio将多个module统一放在一个
  6. Android中Layout转成Java代码
  7. Android热修复方案的兼容策略CLASS_ISPRE
  8. [置顶] Android(安卓)APK优化工具Zipalig
  9. android入门——eros项目的第一个demo
  10. Android(安卓)AccessibilityService机制