一、需求

由于Android系统碎片化比较严重,因此为了统一调整状态栏颜色,因此实现一个工具类相当必要。

注意:本类支持Android 5.0 以上的版本,android 5.0之前的兼容性太差,因此不做处理。

public class StatusBarManager {

private int is_MIUI_platform = -1; //初始值是-1,0表示检测过不是改平台,1表示检测过时该平台

private int is_MEIZU_platform = -1; //初始值是-1,0表示检测过不是改平台,1表示检测过时该平台

public static final int UI_FIT_SYSTEM_WINDOW = 0x01; //全屏模式

public static final int UI_FIT_ACTIVITY = 0x00; //着色模式

private static ColorDrawable colorDrawable;

public static volatile SoftReference statusBarHeightSoftRef = new SoftReference(-1);

public static StatusBarManager shareInstance;

protected StatusBarManager(){

}

private StatusBarManager updateStatusBarTheme(Activity activity, @ColorInt int color, @IntRange(to =UI_FIT_SYSTEM_WINDOW,from=UI_FIT_ACTIVITY) int mode) {

if (activity == null) return null;

if(shouldUsed()){

setStatusBarColor(activity, color,mode);

}

return this;

}

public static boolean shouldUsed() {

return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

}

public static StatusBarManager updateStatusBarTheme(Activity activity, StatusBarAdapter adapter) {

if (activity == null || adapter==null) return null;

final int statusBarColor = adapter.getStatusBarColor();

final int statusBarMode = adapter.getStatusBarMode();

return shareInstance().updateStatusBarTheme(activity,statusBarColor,statusBarMode);

}

public synchronized static StatusBarManager shareInstance() {

if (shareInstance==null){

shareInstance = new StatusBarManager();

}

return shareInstance;

}

public int getStatusBarColor(Activity activity){

if(!shouldUsed()) return Color.BLACK;

Window window = activity.getWindow();

if(window==null) return Color.BLACK;

return window.getStatusBarColor();

}

public int getStatusBarMode(Activity activity){

if(!shouldUsed()) return UI_FIT_ACTIVITY;

Window window = activity.getWindow();

if(window==null) return UI_FIT_ACTIVITY;

final View decorView = activity.getWindow().getDecorView();

if(decorView==null) return UI_FIT_ACTIVITY;

int visibility = decorView.getSystemUiVisibility();

if( (visibility & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)!=0){

return UI_FIT_SYSTEM_WINDOW;

}

return UI_FIT_ACTIVITY;

}

private static void setStatusBarColor(Activity activity, @ColorInt int color,@IntRange(to =UI_FIT_SYSTEM_WINDOW,from=UI_FIT_ACTIVITY) int mode) {

setStatusFeatures(activity,color,mode);

int colorNav;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

colorNav = activity.getResources().getColor(R.color.color_navigation_bar_color, activity.getTheme());

} else {

colorNav = Utils.getResourcesColor(R.color.color_navigation_bar_color);

}

if(colorDrawable==null){

colorDrawable = new ColorDrawable(colorNav);

}else{

colorDrawable.setColor(colorNav);

}

if(activity instanceof AppCompatActivity) {

if (((AppCompatActivity)activity).getSupportActionBar() != null) {

((AppCompatActivity)activity).getSupportActionBar().setBackgroundDrawable(colorDrawable);

}

}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

//设置状态栏颜色

// activity.getWindow().setStatusBarColor(shiftColor(color, 0.9f));

activity.getWindow().setStatusBarColor(color);

//设置导航栏(Actionbar,底部虚拟按键导航)颜色

activity.getWindow().setNavigationBarColor(colorNav);

}

}

private static void setStatusFeatures(Activity activity,@ColorInt int color,int mode) {

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;

Window window = activity.getWindow();

//取消状态栏透明

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

//添加Flag把状态栏设为可绘制模式

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

//设置系统状态栏处于可见状态时位子的颜色

// if(isLightColor(color)) {

int flags =NcfColorUtils.isLightColor(color)?View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : View.SYSTEM_UI_FLAG_LAYOUT_STABLE; //判断是否为亮色,如果为亮色,则文字为黑色,反之为白色

if(mode==UI_FIT_ACTIVITY) {

flags |= View.SYSTEM_UI_FLAG_VISIBLE; //内容区非全屏模式

}else{

flags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; //内容区全屏模式,内容区显示到状态栏下方

}

final View decorView = window.getDecorView();

if(decorView.getSystemUiVisibility()!= flags) {

decorView.setSystemUiVisibility(flags);

}

if(isXiaomi()) {

setMIUIStatusBarDarkMode(NcfColorUtils.isLightColor(color), activity);

}else if(isMeizu()){

setMeizuStatusBar(NcfColorUtils.isLightColor(color), activity);

}

//让view不根据系统窗口来调整自己的布局

ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);

View mChildView = mContentView.getChildAt(0);

if (mChildView != null) {

if(mChildView.getFitsSystemWindows()) {

mChildView.setFitsSystemWindows(false);

}

ViewCompat.requestApplyInsets(mChildView);

}

}

// 设置魅族状态栏

public static void setMeizuStatusBar( boolean isLightStatusBar,Activity activity) {

try {

if(StatusBarManager.shareInstance().is_MEIZU_platform==0) return;

StatusBarManager.shareInstance().is_MEIZU_platform = 0;

final Window window = activity.getWindow();

WindowManager.LayoutParams params = 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(params);

if (isLightStatusBar) {

value |= bit;

} else {

value &= ~bit;

}

meizuFlags.setInt(params, value);

window.setAttributes(params);

darkFlag.setAccessible(false);

meizuFlags.setAccessible(false);

StatusBarManager.shareInstance().is_MEIZU_platform = 1;

} catch (Exception e) {

e.printStackTrace();

StatusBarManager.shareInstance().is_MEIZU_platform = 0;

}

}

public static void setMIUIStatusBarDarkMode(boolean darkmode, Activity activity) {

Class extends Window> clazz = activity.getWindow().getClass();

try {

if(StatusBarManager.shareInstance().is_MIUI_platform==0) return;

int darkModeFlag = 0;

StatusBarManager.shareInstance().is_MIUI_platform = 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);

extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);

StatusBarManager.shareInstance().is_MIUI_platform = 1;

} catch (Exception e) {

StatusBarManager.shareInstance().is_MIUI_platform = 0;

e.printStackTrace();

return;

}

}

// 是否是小米手机

public static boolean isXiaomi() {

return "Xiaomi".equals(Build.MANUFACTURER);

}

// 是否是魅族手机

public static boolean isMeizu() {

try {

Method method = Build.class.getMethod("hasSmartBar");

return method != null;

} catch (NoSuchMethodException e) {

}

return false;

}

public static int getStatusBarHeight(Context context) {

int statusBarHeight = (statusBarHeightSoftRef!=null)?statusBarHeightSoftRef.get():-1;

if(statusBarHeight>0) return statusBarHeight;

Resources res = context.getResources();

int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");

if (resourceId > 0) {

statusBarHeight = res.getDimensionPixelSize(resourceId);

}else{

statusBarHeight = getStatusBarHeightWithoutRef(context);

}

statusBarHeightSoftRef = new SoftReference(statusBarHeight);

return statusBarHeight;

}

public static int getStatusBarHeightWithoutRef(Context context) {

int statusBarHeight = 0;

try {

Class> clazz = Class.forName("com.android.internal.R$dimen");

Object object = clazz.newInstance();

int height = Integer.parseInt(clazz.getField("status_bar_height")

.get(object).toString());

statusBarHeight = context.getResources().getDimensionPixelSize(height);

} catch (Exception e) {

e.printStackTrace();

}

return statusBarHeight;

}

//判断颜色是否为亮色

public static boolean isLightColor(@ColorInt int color){

int red = Color.red(color);

int blue = Color.blue(color);

int green = Color.green(color);

int alpha = Color.alpha(color);

if(alpha<51){

return true;

}

return (red*0.299 + blue*0.578 + green*0.114) >= 192;

}

}

适配器

public interface StatusBarAdapter {

public @IntRange(from=UI_FIT_ACTIVITY,to =UI_FIT_SYSTEM_WINDOW) int getStatusBarMode();

public @ColorInt int getStatusBarColor();

}

参考博客:

https://www.jianshu.com/p/bae25b5eb867

https://www.jianshu.com/p/932568ed31af

https://juejin.im/post/5a30f1535188251c11409d77

https://www.jianshu.com/p/035a7e19fd9b

android 颜色0x00,Android 状态栏颜色兼容方案相关推荐

  1. Android如何设置顶部状态栏颜色(主题)

    在Android中我们经常需要设置屏幕顶部状态栏的主题和应用页面保持同一风格,本文介绍几种常用的设置方案: 状态栏将显示为纯净的颜色,没有渐变效果 ​ /** * 状态栏相关工具类 * */ publ ...

  2. android status_bar_height动态调整,Android沉浸状态栏(StatusBar)兼容方案

    所谓"沉浸状态栏"的实现需要两点: 设置状态栏为透明或者半透明状态; 整体布局可以置于状态栏下方. 1. 状态栏的配置 对于状态栏的配置有两种方式: 在manifest中配置Act ...

  3. android studio开发手机状态栏颜色更改

    使用android studio开发的哥们肯定都知道,as开发的工程,手机状态栏都默认变成蓝色,许多新手都不知道在哪里解决,其实很简单,只需要一行代码就可以 大家都知道androidmanifest中 ...

  4. html设置ios状态栏颜色,iOS 修改状态栏颜色

    iOS中修改状态颜色在iOS9后官方废弃了下面这种方法 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDef ...

  5. Android状态栏颜色修改

    android状态栏颜色修改 状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的. 5.x环境下 方式一,状态栏将显示为纯净的颜色,没有渐变效果 [java] view ...

  6. Android设置顶部状态栏颜色

    参考博客:https://blog.csdn.net/qq_36982160/article/details/82350993 一张图了解android状态栏颜色划分 方法一:通过代码设置状态栏颜色 ...

  7. Android开发技巧——设置系统状态栏颜色

    开门见山,先来三张效果图: 然后我们再来讲如何实现以及如何快速地实现. 如何实现 实现设置系统状态栏颜色需要至少在Android 4.4.2(API 19)以上.这是因为,在这个版本以下,没有任何的A ...

  8. Android 如何更改状态栏颜色

    然后在我们的Activity的onCreate方法中使用以上代码就可以了. 问题及解决方法 但是在使用之后,我们会发现,我们Activity的布局内容顶到了状态栏上去了,被状态栏及ActionBar所 ...

  9. Android 实现App状态栏颜色 白色、透明色

    前言 根据PM要求App首页的跳转过于简单 需要重新设计几个新的页面 UI妹子很快就做好了几套方案交给我和IOS 于是苦逼的日子又来临了,赶代码,修bug,加班,标签云集于脑海. 附效果图: 好了不吐 ...

最新文章

  1. 项望烽:移动 IM 开发之登录优化
  2. 负载均衡和CDN技术
  3. 如何启动mongoDB并用Robo 3T连接
  4. WPF 类型“System.ComponentModel.ISupportInitialize”在未被引用的程序集中定义。
  5. [学习笔记] 如果你愿意学那么你是可以看的懂的 —— 群论与 burnside 引理和 polya 定理
  6. VUE3.x(v-model)数据双向绑定指令
  7. 修改目录标题层级_关键词所在页面的层级越高权重越大
  8. STM32工作笔记0044---什么是二极管什么是三极管
  9. android 应用搬家 分区,把安装在SD卡的应用存在DATA分区的数据移到SD卡上
  10. 07 SQL优化技术
  11. 数据结构与算法必备的 50 个代码实现。
  12. QT QML 3D模型查看器
  13. 计算机三级网络技术 = =
  14. python把英语句子成分字母_python把英语句子成分字母
  15. kafka:sync、async以及oneway 几种发送消息模式
  16. 3DText无法被物体遮挡 - 解决
  17. linux winscp 乱码,WinSCP无法登陆、乱码及关联Putty的设置
  18. 服务器基线加固脚本_一种基于WebLogic的安全基线加固方法与流程
  19. usermode linux网络空间,(RHCE笔记)linux基础之三 用户、组及权限
  20. 【转】JS VLC插件

热门文章

  1. Ubuntu 使用 Smina 对接教程
  2. R every day !
  3. CTF SQL注入知识点
  4. 东大OJ-1391-Big big Power
  5. Java安装配置环境变量及介绍数据类型
  6. 红皮书--EOF与BOF
  7. 算法应用-百钱买百鸡
  8. python学习:删除空白
  9. leetcode 61 Rotate List ----- java
  10. Oracle命令--表空间管理