Android修改状态栏颜色(已适配)

通过反射获取到状态栏的高度。代码注释都有。直接调用就行






import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2017-2018, by zhaojl, All rights reserved.
 * -----------------------------------------------------------------
 * Desc: 描述 状态栏图标颜色
 * File: NXStatusBar.java
 * Version: v1.0
 * Create: 2018/10/31 21:53
 * 

* Changes (from 2018/10/31) * ----------------------------------------------------------------- * 2018/10/31 : Create NXStatusBar.java (zhaojl); * ----------------------------------------------------------------- */ public class NXStatusBar { public static void setStatusBarFull(Activity activity) { //检查是否 Android 5.0 以上版本 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { View decorView = activity.getWindow().getDecorView(); int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } } private static final int MIUI = 1; private static final int FLYME = 2; private static final int ANDROID_M = 3; public static void setTransparentForWindow(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow() .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } /** * 修改状态栏为全透明且不占位 */ @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); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(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); } } /** * 添加状态栏占位视图 * 在去掉状态栏之后,内容会和状态栏重叠 加上这个来占位 */ public static void addStatusView(Activity activity, ViewGroup contentView) { View statusBarView = new View(activity); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); // statusBarView.setBackgroundColor(ContextCompat.getColor(activity, R.color.status)); statusBarView.setBackgroundColor(Color.BLACK); statusBarView.setLayoutParams(lp); contentView.addView(statusBarView, 0); } /** * 利用反射获取状态栏高度 */ private static int getStatusBarHeight(Activity activity) { int result = 0; //获取状态栏高度的资源id int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = activity.getResources().getDimensionPixelSize(resourceId); } return result; } /** * 状态栏亮色模式,设置状态栏黑色文字、图标, * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android * * @return 1:MIUUI 2:Flyme 3:android6.0 */ public static int setStatusBarLightMode(Activity activity) { int result = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 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 = ANDROID_M; } else if (FLYMESetStatusBarLightMode(activity.getWindow(), true)) { result = FLYME; } else if (MIUISetStatusBarLightMode(activity, true)) { result = MIUI; } } return result; } public static int setStatusBarDarkMode(Activity activity) { int result = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { View decorView = activity.getWindow().getDecorView(); if (decorView != null) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_VISIBLE); } } result = ANDROID_M; } else if (MIUISetStatusBarLightMode(activity, false)) { result = MIUI; } else if (FLYMESetStatusBarLightMode(activity.getWindow(), false)) { result = FLYME; } } return result; } /** * 已知系统类型时,设置状态栏黑色文字、图标。 * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android * * @param type 1:MIUUI 2:Flyme 3:android6.0 */ public static void setStatusBarLightMode(Activity activity, int type) { if (type == MIUI) { MIUISetStatusBarLightMode(activity, true); } else if (type == FLYME) { FLYMESetStatusBarLightMode(activity.getWindow(), true); } else if (type == ANDROID_M) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } } /** * 状态栏暗色模式,清除MIUI、flyme或6.0以上版本状态栏黑色文字、图标 */ public static void setStatusBarDarkMode(Activity activity, int type) { if (type == MIUI) { MIUISetStatusBarLightMode(activity, false); } else if (type == FLYME) { FLYMESetStatusBarLightMode(activity.getWindow(), false); } else if (type == ANDROID_M) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } } /** * 设置状态栏图标为深色和魅族特定的文字风格 * 可以用来判断是否为Flyme用户 * * @param window 需要设置的窗口 * @param dark 是否把状态栏文字及图标颜色设置为深色 * @return boolean 成功执行返回true */ private 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 dark 是否把状态栏文字及图标颜色设置为深色 * @return boolean 成功执行返回true */ private static boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) { boolean result = false; Window window = activity.getWindow(); 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; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错,所以两个方式都要加上 if (dark) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } else { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } } } catch (Exception e) { e.printStackTrace(); } } return result; } public static void changeStatusBar(Activity activity, ViewGroup contentView) { int result = setStatusBarLightMode(activity); // 如果是0的情况 则未变成黑色模式 if (result == 0) { addStatusView(activity, contentView); } else { addStatusViewWhite(activity, contentView); } } /** * 添加白底状态栏占位视图 * 在去掉状态栏之后,内容会和状态栏重叠 加上这个来占位 */ public static void addStatusViewWhite(Activity activity, ViewGroup contentView) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { View statusBarView = new View(activity); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); // statusBarView.setBackgroundColor(ContextCompat.getColor(activity, R.color.white)); statusBarView.setBackgroundColor(Color.WHITE); statusBarView.setLayoutParams(lp); contentView.addView(statusBarView, 0); } } }

你可能感兴趣的:(Android修改状态栏颜色(已适配))