Android StatusBar 的颜色

 Android 5.0以后支持statusbar 更换颜色,6.0以后支持夜间模式

  /**
     * 设置状态栏的颜色
     */
    protected void setStatusBarColor(int color, boolean lightTheme) {
        if (lightTheme) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP &&
                    Build.VERSION.SDK_INT < Build.VERSION_CODES.M &&
                    !BaseDeviceUtils.isXiaoMi()) {
                StatusBarUtil.setStatusBarColor(this, color, 40);
            } else {
                StatusBarUtil.setStatusBarColor(this, color, 0);
                setStatusBarTheme(false);
            }
        } else {
            StatusBarUtil.setStatusBarColor(this, color, 0);
            setStatusBarTheme(true);
        }
    }

    public void setStatusBarTheme(boolean darkMode) {
        //原生6.0以上有API支持黑条状态栏图标,但是国产各ROM经过定制,
        // 有的需要特定的设置才能实现
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = getWindow().getDecorView();
            int systemuiVisibility = decorView.getSystemUiVisibility();
            if (darkMode) {
                decorView.setSystemUiVisibility(systemuiVisibility & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                decorView.setSystemUiVisibility(systemuiVisibility | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        }
        this.setupStatusBarForOtherSystem(darkMode);
    }

    protected int getCustomStatusBarColor() {
        if (isLightTheme()) {
            return getResources().getColor(R.color.status_bar_color_light);
        }
        return getResources().getColor(R.color.status_bar_color_dark);
    }

    protected void setupStatusBarForOtherSystem(boolean dark) {
        StatusBarUtil.setupStatusBarForOtherSystem(this, !dark);
    }
public class StatusBarUtil {

    /**
     * 计算状态栏颜色
     *
     * @param color color值
     * @param alpha alpha值
     * @return 最终的状态栏颜色
     */
    private static int calculateStatusColor(@ColorInt int color, int alpha) {
        if (alpha == 0) {
            return color;
        }
        float a = 1 - alpha / 255f;
        int red = color >> 16 & 0xff;
        int green = color >> 8 & 0xff;
        int blue = color & 0xff;
        red = (int) (red * a + 0.5);
        green = (int) (green * a + 0.5);
        blue = (int) (blue * a + 0.5);
        return 0xff << 24 | red << 16 | green << 8 | blue;
    }

    /**
     * 设置状态栏颜色
     *
     * @param activity       需要设置的activity
     * @param color          状态栏颜色值
     * @param statusBarAlpha 状态栏透明度
     */

    public static void setStatusBarColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));
        }
    }


    public static boolean setupStatusBarForOtherSystem(Activity activity, boolean setDarkMode) {
        if (BaseDeviceUtils.isXiaoMi()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Class<? extends Window> windowClass = activity.getWindow().getClass();
                try {
                    Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager.LayoutParams");
                    int darkmode = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE").getInt(layoutParams);
                    Method setExtraFlags = windowClass.getMethod("setExtraFlags", Integer.TYPE, Integer.TYPE);
                    int mode = setDarkMode ? darkmode : 0;
                    setExtraFlags.invoke(activity.getWindow(),mode,darkmode);
                } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

}
 public static boolean isXiaoMi() {
        return Build.MANUFACTURER.equalsIgnoreCase("xiaomi");
    }

不同的厂商可能实现不同,实测魅族可以。

最好封装到BaseActivity 里面,在初始化(setContentView 之前)的时候去改变statusbar 的颜色或者模式。

Activity 的setContentView 会先创建 DecorView 在创建DecorView 的时候会 根据Theme 去加载不同的ViewParent(RootView)(所以Theme在setContentView 之后调用无效),然后add进DecorView.一般rootview 包含了 Title 和content 两部分,setContentView就是将

传进来的view 添加到content。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值