Android设置沉浸式状态栏时改变状态栏的颜色(只对MIUI V6可用)

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();
    }

}