700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 更改小米 魅族手机状态栏字体颜色

更改小米 魅族手机状态栏字体颜色

时间:2021-04-23 04:36:03

相关推荐

更改小米 魅族手机状态栏字体颜色

以前在githup找了一个工具类,状态栏字体颜色是白色的时候没有什么问题,

但是改为深颜色后,发现小米5手机是没有生效的,后面在小米开发文档看了一下,发现

6.0以后不用为小米的状态栏字体做处理了,下面是工具类,标红的地方是处理小米5以上的

状态栏字体颜色

手机类型

private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

/** * 判断手机是否是小米 * * @return */public static boolean isMIUI() {try {final BuildProperties prop = BuildProperties.newInstance(); return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null; } catch (final IOException e) {return false; }}/** * 判断手机是否是魅族 * * @return */public static boolean isFlyme() {try {// Invoke Build.hasSmartBar() final Method method = Build.class.getMethod("hasSmartBar"); return method != null; } catch (final Exception e) {return false; }}

/** * @param activity * @param useThemestatusBarColor 是否要状态栏的颜色,不设置则为透明色 * @param withoutUseStatusBarColor 是否不需要使用状态栏为暗色调 */public static void setStatusBar(Activity activity, boolean useThemestatusBarColor, boolean withoutUseStatusBarColor) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上 View decorView = activity.getWindow().getDecorView(); int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); if (useThemestatusBarColor) {activity.getWindow().setStatusBarColor(activity.getResources().getColor(R.color.white)); } else {activity.getWindow().setStatusBarColor(Color.TRANSPARENT); }} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0 WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes(); localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags); }if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !withoutUseStatusBarColor) {activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); }}

/** * 改变魅族的状态栏字体为黑色,要求FlyMe4以上 */private static void processFlyMe(boolean isLightStatusBar, Activity activity) {WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); try {Class<?> instance = Class.forName("android.view.WindowManager$LayoutParams"); int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp); Field field = instance.getDeclaredField("meizuFlags"); field.setAccessible(true); int origin = field.getInt(lp); if (isLightStatusBar) {field.set(lp, origin | value); } else {field.set(lp, (~value) & origin); }} catch (Exception ignored) {ignored.printStackTrace(); }}

/** * 改变小米的状态栏字体颜色为黑色, 要求MIUI6以上 lightStatusBar为真时表示黑色字体 */private static void processMIUI(boolean lightStatusBar, Activity activity) {Class<? extends Window> clazz = activity.getWindow().getClass(); try {int darkModeFlag; 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(), lightStatusBar ? darkModeFlag : 0, darkModeFlag); } catch (Exception ignored) {ignored.printStackTrace(); }}

/** * 需要MIUIV6以上 * * @param activity * @param dark是否把状态栏文字及图标颜色设置为深色 * @return boolean 成功执行返回true */public 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) {}}return result;}/** * 设置状态栏图标为深色和魅族特定的文字风格 * 可以用来判断是否为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;}

/** * 设置状态栏文字色值为深色调 * * @param useDart 是否使用深色调 * @param activity */@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)public static void setStatusTextColor(boolean useDart, Activity activity) {if (isFlyme()) {processFlyMe(useDart, activity); } else if (isMIUI()) { //6.0后小米状态栏用的原生的if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (useDart) {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);}} else {activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); }activity.getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, navigationHeight); } else {processMIUI(useDart, activity); }} else {if (useDart) {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); }} else {activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE); }activity.getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, navigationHeight); }}/** * 设置状态栏文字色值为深色调 * * @param useDart 是否使用深色调 * @param activity */public static void setStatusTextColorNew(boolean useDart, Activity activity) {if (isFlyme()) {FlymeSetStatusBarLightMode(activity.getWindow(), useDart); //processFlyMe(useDart, activity); } else if (isMIUI()) {MIUISetStatusBarLightMode(activity, useDart); } else {if (useDart) {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); }} else {activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); }activity.getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, navigationHeight); }}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。