Created
September 22, 2017 06:15
-
-
Save lxxself/4c553438cbb908d7b3dc3d65bc6f98a5 to your computer and use it in GitHub Desktop.
状态栏颜色
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.homework.app.util; | |
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.graphics.Color; | |
import android.os.Build; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
/** | |
* Created by wxy on 2017/7/3. | |
*/ | |
public class SystemBarUtil { | |
//白色可以替换成其他浅色系 | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public static void myStatusBar(Activity activity) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
if (MIUISetStatusBarLightMode(activity.getWindow(), true) && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {//MIUI 且 android版本低于6.0 | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 | |
activity.getWindow().setStatusBarColor(Color.WHITE); | |
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 | |
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, | |
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | |
SystemBarTintManager tintManager = new SystemBarTintManager(activity); | |
tintManager.setStatusBarTintEnabled(true); | |
tintManager.setStatusBarTintResource(android.R.color.white); | |
} | |
} else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {//Flyme | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 | |
activity.getWindow().setStatusBarColor(Color.WHITE); | |
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 | |
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, | |
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | |
SystemBarTintManager tintManager = new SystemBarTintManager(activity); | |
tintManager.setStatusBarTintEnabled(true); | |
tintManager.setStatusBarTintResource(android.R.color.white); | |
} | |
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0 | |
activity.getWindow().setStatusBarColor(Color.WHITE); | |
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); | |
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
activity.getWindow().setStatusBarColor(Color.GRAY); | |
} | |
} | |
} | |
/** | |
* 设置状态栏字体图标为深色,需要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; | |
} | |
/** | |
* 设置状态栏图标为深色和魅族特定的文字风格 | |
* 可以用来判断是否为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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment