状态栏颜色

关于状态栏的设置,网上一大堆教程、方法,有些有效有些无效,太多太杂,所以想总结一下:
参考 :https://www.likecs.com/show-821162.html

1. 修改状态栏的颜色

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.WHITE);

setStatusBarColor() 是API Level 21()引入的方法,但是仅仅调用这个方法是无效的,还需要添加FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 标记

1. 修改状态栏字体的颜色

状态栏里的时间、电量等信息字体颜色是白色,状态栏背景被设置成了白色后会导致看不到这些信息,所以浅色背景下需要让文字颜色设置成暗色
API Level 23 以上

window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

字体颜色设置成黑色,这种方式只能在api>=23使用,api <19是没办法的,23>api>19 有些品牌的手机可以通过反射修改系统参数达到目的

miui系统(小米手机):

public static boolean miuiSetStatusBarLightMode(Activity activity, boolean trans) {
        boolean result = false;

        if (activity == null) return result;

        Window window = activity.getWindow();
        if (window == null) return result;

        Class clazz = window.getClass();
        try {
            int tranceFlag;
            int darkModeFlag;
            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 (trans) {
                extraFlagField.invoke(window, tranceFlag | darkModeFlag, tranceFlag | darkModeFlag);
            } else {
                //只黑色
                extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
            }
            result = true;
        } catch (Exception e) {
            TLog.e(e);
        }
        return result;
    }

flyme系统(魅族手机)

@TargetApi(Build.VERSION_CODES.KITKAT)
    public static boolean flymeSetImmersedWindow(Window window, boolean immersive) {
        boolean result = false;
        if (!DeviceUtil.isMeizuFlymeOS()) {
            return result;
        }
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            int trans_status = 0;
            Field flags;
            if (Build.VERSION.SDK_INT < 19) {
                try {
                    trans_status = 1 << 6;
                    flags = lp.getClass().getDeclaredField("meizuFlags");
                    flags.setAccessible(true);
                    int value = flags.getInt(lp);
                    if (immersive) {
                        value = value | trans_status;
                    } else {
                        value = value & ~trans_status;
                    }
                    flags.setInt(lp, value);
                    result = true;
                } catch (Exception e) {
                    TLog.e(e);
                }
            } else {
                lp.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                window.setAttributes(lp);
                result = true;
            }
        }
        return result;
    }

你可能感兴趣的:(状态栏颜色)