Android应用改变状态栏颜色和APP风格保持一致

来源:互联网 发布:lua脚本php 编辑:程序博客网 时间:2024/05/22 15:29

第1步:

在项目moudle下的gradle文件里添加以下依赖
compile 'com.githang:status-bar-compat:0.3'
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
第2步:
实现一个改变状态栏颜色的工具类
public class ChangeStatusBarColor {    public static void initSystemBar(Activity activity) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            setTranslucentStatus(activity, true);        }        SystemBarTintManager tintManager = new SystemBarTintManager(activity);        tintManager.setStatusBarTintEnabled(true);        // 使用颜色资源        tintManager.setStatusBarTintResource(R.color.colorAccent);    }    @TargetApi(19)    private static void setTranslucentStatus(Activity activity, boolean on) {        Window win = activity.getWindow();        WindowManager.LayoutParams winParams = win.getAttributes();        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;        if (on) {            winParams.flags |= bits;        } else {            winParams.flags &= ~bits;        }        win.setAttributes(winParams);    }}

第3步:

在需要改变状态栏颜色的地方调用改变状态栏颜色的方法

//改变状态栏的颜色ChangeStatusBarColor.initSystemBar(this);


阅读全文
0 0