前言:Android开发,对于状态栏的修改,实在是不友好,没什么api可以用,不像iOS那么方便.但是ui又喜欢只搞ios一套.没办法.各种翻源码,写反射.真的蛋疼.需求场景:当toolbar及状态栏需要为白色或浅色时(如简书),状态栏由于
前言:
Android开发,对于状态栏的修改,实在是不友好,没什么api可以用,不像iOS那么方便.但是ui又喜欢只搞ios一套.没办法.各种翻源码,写反射.真的蛋疼.
需求场景:
当toolbar及状态栏需要为白色或浅色时(如简书),状态栏由于用的Light风格Theme,字体,图标也都是白色,会看不清.如果改变成黑色就很和谐了.
一.修改状态栏颜色:
改变状态栏颜色,可以看看这篇文章.传送门:实现状态栏(statusbar)渐变效果其实很简单
传送门实现的效果:
这种方法实现的状态栏变色,没有黑色背景.
使用全屏模式实现的效果如下(QQ的效果):
很明显的黑色背景.
我用的手机是华为,系统7.0
二.修改状态栏字体:
通用工具类:
public class StatusBarUtil { public static int getStatusBarLightMode(Window window) { int result = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (MIUISetStatusBarLightMode(window, true)) { result = 1; } else if (FlymeSetStatusBarLightMode(window, true)) { result = 2; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); result = 3; } else {//5.0 } } return result; } public static void setStatusBarLightMode(Window window) { int type = getStatusBarLightMode(window); if (type == 1) { MIUISetStatusBarLightMode(window, true); } else if (type == 2) { FlymeSetStatusBarLightMode(window, true); } else if (type == 3) { window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } else {//5.0 } } public static void StatusBarDarkMode(Window window) { int type = getStatusBarLightMode(window); if (type == 1) { MIUISetStatusBarLightMode(window, false); } else if (type == 2) { FlymeSetStatusBarLightMode(window, false); } else if (type == 3) { window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } } 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; } 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; }}
--结束END--
本文标题: Android实现修改状态栏背景、字体和图标颜色的方法
本文链接: https://www.lsjlt.com/news/221037.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0