设置沉浸式状态栏

设置沉浸式状态栏:

    /**
     * 设置沉浸式状态栏,并把状态栏颜色改为透明色
     */
    public static void setStatusBar(Activity activity) {
        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_LAYOUT_STABLE;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                option += View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            decorView.setSystemUiVisibility(option);
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }

设置状态栏的背景色和给title设置间距:

    /**
     * 设置状态栏的背景色为title的背景色,如果有title,给title增加状态栏间距
     *
     * @param context   context
     * @param titleView titleView
     *                  调用示例
     *                  WindowUtils.setStatusTitle(getContext(), bindId(R.id.ctl_title));
     */
    public static void setStatusTitle(Context context, View titleView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (titleView != null) {
                int statusHeight = getStatusBarHeight(context);
                ViewGroup.LayoutParams layoutParams = titleView.getLayoutParams();
                //设置间距
                setLayoutMargin(layoutParams, 0, statusHeight, 0, 0);
                //设置状态栏的背景色
                ColorDrawable colorDrawable = (ColorDrawable) titleView.getBackground();
                if (colorDrawable != null) {
                    //设置状态栏背景色为标题背景色
                    ((Activity) context).getWindow().setStatusBarColor(colorDrawable.getColor());
                }
            } else {
                //设置状态栏背景色为透明色
                ((Activity) context).getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        }
    }

 

你可能感兴趣的:(AndroidStudio)