给状态栏设置渐变颜色

来源:互联网 发布:缓存服务器软件 编辑:程序博客网 时间:2024/05/16 16:07
1 状态栏设置纯颜色,很方便,直接获取状态栏,设置颜色即可代码:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {Window window = ((Activity)context).getWindow();window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);//window.setStatusBarColor(context.getResources().getColor(colorId)); //纯颜色设置//底部导航栏//window.setNavigationBarColor(context.getResources().getColor(R.color.titlecolor));}2 那想让状态栏是渐变色该怎么处理呢?这又分两种情况了,一种是继承传统的 Activity ,一种是继承 AppCompatActivity。(1) 继承 Activity ,这种方式,谷歌没有提供给状态栏设置drawable 的方法,所以,只能利用反射去设置View statusBarView ;public void setStatuesBg(){ if (statusBarView == null) {            int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");            statusBarView = getWindow().findViewById(identifier);        }        if (statusBarView != null) {            statusBarView.setBackgroundResource(R.drawable.topcolor_grient);            isset = true;//            isStatusBar(true);        }        }topcolor_grient:  这个就是自己设置的的渐变背景<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <gradient        android:angle="180"        android:endColor="@color/color_2196"        android:startColor="@color/color_1655" /></shape>(2) 如果是继承AppCompatActivity,那就好处理了,原理就是把状态栏设为透明色,然后把布局订到状态栏下面,ok,这时候需要注意的就是调整布局和状态栏的距离了。代码:public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,        View needOffsetView) {        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {            return;        }        setTransparentForWindow(activity);        addTranslucentView(activity, statusBarAlpha);        if (needOffsetView != null) {            Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET);            if (haveSetOffset != null && (Boolean) haveSetOffset) {                return;            }            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();            layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity),                layoutParams.rightMargin, layoutParams.bottomMargin);            needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true);        }    }      /**     * 设置透明     */    private static void setTransparentForWindow(Activity activity) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);            activity.getWindow()                .getDecorView()                .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            activity.getWindow()                .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        }    }  /**     * 添加半透明矩形条     *     * @param activity       需要设置的 activity     * @param statusBarAlpha 透明值     */    private static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {        ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);        View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);        if (fakeTranslucentView != null) {            if (fakeTranslucentView.getVisibility() == View.GONE) {                fakeTranslucentView.setVisibility(View.VISIBLE);            }            fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));        } else {            contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));        }    } 总结:文章编辑的有点乱,稍后会有demo,如果这段时间有人需要的话,留下邮箱,我给你们发过去。
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝主营类目怎么改 主营业务收入借贷 主营业务成本包括什么 主营业务成本率 主营业务收入属于什么类科目 主营业务税金及附加包括哪些 主营业务利润英文 利润表主营业务成本 主营业务收入 借贷 快穿之女主只想营业 快穿女主只想营业 淘宝店铺怎么修改主营类目 主见 有主见 主见是什么意思 没有主见怎么办 孩子没主见要怎么培养 一个人没有主见到底有多可怕 偏执男主一见钟情女主 男主一见钟情强取豪夺 法考主观题 主观题 主观能动性 主观客观 主观 主观唯心 客观主观 主观题成绩 主观臆断 主观意识 2019主观题 主观和客观 主观意思 主观主义 主观唯心主义 法考主观题成绩公布时间 暴食巫主观海公子 司法考试主观题 2019法考主观题 主观能动性什么意思 中川美香主观高清在线