最近想着让自己做的app看起来好看点。。所以给自己的app添加了可以变色的状态栏。。我没有做向下的兼容。。。只能手机版本是4.4及以上的手机使用。网上有人叫沉浸式,我觉着这边博主的解释是正确的:
Android 实现变色状态栏

特别重要的一步:
Activity一定要使用一种主题!

android:theme="@style/AppTheme.NoActionBar"

这里我使用的是默认的主题,看你自己需要使用。

1,首先在activity的onCreate方法中,将标题栏设置为透明:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,         WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        }

2,然后为了不让自己的顶部控件向上移动,需要在布局文件的顶部控件里面添加以下两句话:

 android:fitsSystemWindows="true" android:clipToPadding="true"

3,然后如果你的顶部控件的背景颜色是蓝色,那么你的标题栏也会变成蓝色:
可以看下以下的效果图:
android 变色状态栏_第1张图片

分享:
这里是一位简书上的大神写的一个工具类我用着挺好。。。分享给大家。。也谢谢大神的分享:
需要自行下载一个开源库SystemBarTint
有时间我也要把这个开源库的源码看一下,学习一下!

public class StatusBarUtil {    /**     * 修改状态栏为全透明     *     * @param activity     */    @TargetApi(19)    public static void transparencyBar(Activity activity) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            Window window = activity.getWindow();            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);            window.setStatusBarColor(Color.TRANSPARENT);            window.setNavigationBarColor(Color.TRANSPARENT);        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            Window window = activity.getWindow();            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        }    }    /**     * 修改状态栏颜色,支持4.4以上版本     *     * @param activity     * @param colorId     */    public static void setStatusBarColor(Activity activity, int colorId) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            Window window = activity.getWindow();//      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);            window.setStatusBarColor(activity.getResources().getColor(colorId));        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            //使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明            transparencyBar(activity);            SystemBarTintManager tintManager = new SystemBarTintManager(activity);            tintManager.setStatusBarTintEnabled(true);            tintManager.setNavigationBarTintEnabled(true);            tintManager.setStatusBarTintResource(colorId);        }    }    /**     * 设置状态栏黑色字体图标,     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android     *     * @param activity     * @return 1:MIUUI 2:Flyme 3:android6.0     */    public static int StatusBarLightMode(Activity activity) {        int result = 0;        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            if (MIUISetStatusBarLightMode(activity.getWindow(), true)) {                result = 1;            } else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {                result = 2;            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);                result = 3;            }        }        return result;    }    /**     * 已知系统类型时,设置状态栏黑色字体图标。     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android     *     * @param activity     * @param type     1:MIUUI 2:Flyme 3:android6.0     */    public static void StatusBarLightMode(Activity activity, int type) {        if (type == 1) {            MIUISetStatusBarLightMode(activity.getWindow(), true);        } else if (type == 2) {            FlymeSetStatusBarLightMode(activity.getWindow(), true);        } else if (type == 3) {            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View                .SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);        }    }    /**     * 清除MIUI或flyme或6.0以上版本状态栏黑色字体     */    public static void StatusBarDarkMode(Activity activity, int type) {        if (type == 1) {            MIUISetStatusBarLightMode(activity.getWindow(), false);        } else if (type == 2) {            FlymeSetStatusBarLightMode(activity.getWindow(), false);        } else if (type == 3) {            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);        }    }    /**     * 设置状态栏图标为深色和魅族特定的文字风格     * 可以用来判断是否为Flyme用户     *     * @param window 需要设置的窗口     * @param dark   是否把状态栏字体及图标颜色设置为深色     * @return boolean 成功执行返回true     */    public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {        boolean result = false;        if (window != null) {            try {                WindowManager.LayoutParams lp = window.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 (dark) {                    value |= bit;                } else {                    value &= ~bit;                }                meizuFlags.setInt(lp, value);                window.setAttributes(lp);                result = true;            } catch (Exception e) {            }        }        return result;    }    /**     * 设置状态栏字体图标为深色,需要MIUIV6以上     *     * @param window 需要设置的窗口     * @param dark   是否把状态栏字体及图标颜色设置为深色     * @return boolean 成功执行返回true     */    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {        boolean result = false;        if (window != null) {            Class clazz = window.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);                if (dark) {                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体                } else {                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体                }                result = true;            } catch (Exception e) {            }        }        return result;    }}

这个工具类具体的功能作者备注的也很详细!
我写了一个小demo,需要的可以下载看看~
https://github.com/slyjs/StatusTintDemo.git

需要学习的好多啊思密达!!!!
android 变色状态栏_第2张图片

更多相关文章

  1. Android获取系统的硬件信息、系统版本以及如何检测ROM类型
  2. 分析点击android桌面app图标启动应用程序的过程
  3. android 兼容各个版本 读取通讯录
  4. Android(1.1-4.2) platform 开发包【全版本】
  5. 火爆新东西,仿QQ版本的ResideMenuItem框架(最新QQ版本的)
  6. 版本更新
  7. Ubuntu 12.04 Desktop 版本编译 Android 4.0.4 出错解决
  8. android edittext password hint字体不同于别的字体的解决

随机推荐

  1. Android(安卓)Studio 2.0 Preview发布,附
  2. Android(安卓)MimeType和MimeTypeMap的介
  3. Android(安卓)中文api (81)——InputMeth
  4. Android短信的发送和广播接收者实现短信
  5. android开发每日汇总【2011-11-02】
  6. Textview基本属性及功能
  7. 解决 Android(安卓)模拟器 无法上网问题
  8. Android带播放进度条的音乐播放器
  9. android 上调试动态库方法
  10. Android一些好的资源