51dev.com IT技术开发者社区

51dev.com 技术开发者社区

Android App 改变状态栏颜色

Android互联网报道阅读(251)2018-04-08 收藏0次评论

//修改状态栏字体颜色
public static void setStatusBarLightMode(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        //判断是否为小米或魅族手机,如果是则将状态栏文字改为黑色
        if (MIUISetStatusBarLightMode(activity, true) || FlymeSetStatusBarLightMode(activity, true)) {
            //设置状态栏为指定颜色
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0

                View decorView = activity.getWindow().getDecorView();
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                decorView.setSystemUiVisibility(option);
                activity.getWindow().setStatusBarColor(Color.TRANSPARENT);

            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4
                //调用修改状态栏颜色的方法
               // setStatusBarColor(activity, color);
                WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //如果是6.0以上将状态栏文字改为黑色

            View decorView = activity.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            decorView.setSystemUiVisibility(option);
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);

        }
    }
}
//检查魅族系统
public static boolean FlymeSetStatusBarLightMode(Activity activity, boolean darkmode) {
    boolean result = false;
    try {
        WindowManager.LayoutParams lp = activity.getWindow().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 (darkmode) {
            value |= bit;
        } else {
            value &= ~bit;
        }
        meizuFlags.setInt(lp, value);
        activity.getWindow().setAttributes(lp);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
//检查小米系统
public static boolean MIUISetStatusBarLightMode(Activity activity, boolean darkmode) {
    boolean result = false;
    Class<? extends Window> clazz = activity.getWindow().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);
        extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

 

以上就是Android App 改变状态栏颜色的全部内容,请多关注【51DEV】IT技术开发者社区。