设置沉浸式状态栏

众所周知 一般设置沉浸式状态栏会有三种方法。第一种系统方法,第二种通过反射,第三种就是通过第三方库。

这里我反过来说一下,先说第三种

1、通过使用第三方库

compile 'com.jaeger.statusbarutil:library:1.4.0'

然后就只需要一句代码就够了

StatusBarUtil.setTranslucentForCoordinatorLayout(activity, 0);

这是一种将头部延伸至状态栏的方法,当然你如果只是想要设置纯颜色的话也有方法可以使用

StatusBarUtil.setColor(activity, 0);//0为颜色值

2、通过反射,这里我所说的是将状态栏设置成白色黑图标

因为需要判断机型所以先添加机型配置的方法

 public static final String SYS_EMUI = "sys_emui";
    public static final String SYS_MIUI = "sys_miui";
    public static final String SYS_FLYME = "sys_flyme";
    public static final String SYS_OTHER = "sys_other";
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
    private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
    private static final String KEY_EMUI_VERSION = "ro.build.version.emui";
    private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";

    public static String getSystem() {
        String SYS = "";
        try {
            Properties prop = new Properties();
            prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
            if (prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
                    || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
                    || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null) {
                SYS = SYS_MIUI;//小米
            } else if (prop.getProperty(KEY_EMUI_API_LEVEL, null) != null
                    || prop.getProperty(KEY_EMUI_VERSION, null) != null
                    || prop.getProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, null) != null) {
                SYS = SYS_EMUI;//华为
            } else if (getMeizuFlymeOSFlag().toLowerCase().contains("flyme")) {
                SYS = SYS_FLYME;//魅族
            } else {
                SYS = SYS_OTHER;//魅族
            }
        } catch (IOException e) {
            e.printStackTrace();
            return SYS;
        }
        return SYS;
    }

    public static String getMeizuFlymeOSFlag() {
        return getSystemProperty("ro.build.display.id", "");
    }

    private static String getSystemProperty(String key, String defaultValue) {
        try {
            Class<?> clz = Class.forName("android.os.SystemProperties");
            Method get = clz.getMethod("get", String.class, String.class);
            return (String) get.invoke(clz, key, defaultValue);
        } catch (Exception e) {
        }
        return defaultValue;
    }
    /**
     * 设置状态栏图标为深色和魅族特定的文字风格
     * 可以用来判断是否为Flyme用户
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    public 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;
    }
//对于android6.0
    //设置成白色的背景,字体颜色为黑色。
    public static void setSixStatusBarDarkMode(Activity activity, boolean dark) {
        if (dark) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(activity.getResources().getColor(android.R.color.white));
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        } else {
            StatusBarUtil.setTranslucentForCoordinatorLayout(activity, 0);
        }

    }

    //对于android6.0
    //设置成头部颜色延伸的背景,字体颜色为黑色。
    public static void setSixStatusBarDarkModeForMain(Activity activity, boolean dark) {
        if (dark) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                StatusBarUtil.setTranslucentForCoordinatorLayout(activity, 0);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        } else {
            StatusBarUtil.setTranslucentForCoordinatorLayout(activity, 0);
        }

    }

    /**
     * 设置状态栏字体图标为深色,需要MIUIV6以上
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
        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 (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

这里方法都贴出来了,但是根据我的配置所得的结果MIUI设置MIUISetStatusBarLightMode不适应所有的小米机型。

//这种方法是白色状态栏、黑色图标
public static void JudgmentSystemManufacturers(Activity activity, boolean dark) {
        if (getSystem().equals(SYS_MIUI)) {
            setSixStatusBarDarkMode(activity, dark);
        } else if (getSystem().equals(SYS_FLYME)) {
            FlymeSetStatusBarLightMode(activity.getWindow(), dark);
        } else {
            setSixStatusBarDarkMode(activity, dark);
        }
    }
//这种方法是状态栏颜色由头部所决定、黑色图标
    public static void JudgmentSystemManufacturersForMain(Activity activity, boolean dark) {
        if (getSystem().equals(SYS_MIUI)) {
            setSixStatusBarDarkModeForMain(activity, dark);
        } else if (getSystem().equals(SYS_FLYME)) {
            FlymeSetStatusBarLightMode(activity.getWindow(), dark);
        } else {
            setSixStatusBarDarkModeForMain(activity, dark);
        }
    }

最后是使用方法,只需要在BaseActivity的onCreate中设置以下代码即可完成全局配置。如果你不想使用全局,那么在想要使用的地方使用就好了

MyUtils.JudgmentSystemManufacturers(this,true);

3、通过系统方法

这种方法网上很多,大同小异就不进行细说了。

猜你喜欢

转载自blog.csdn.net/Painend/article/details/81476755