1.StatusBarUtil
public class StatusBarUtil {
/**
* The constant TYPE_MIUI.
*/
public final static int TYPE_MIUI = 0;
/**
* The constant TYPE_FLYME.
*/
public final static int TYPE_FLYME = 1;
/**
* The constant TYPE_M.
*/
public final static int TYPE_M = 3;//6.0
/**
* The interface View type.
*/
@IntDef({TYPE_MIUI,
TYPE_FLYME,
TYPE_M})
@Retention(RetentionPolicy.SOURCE)
@interface ViewType {
}
/**
* 修改状态栏颜色,支持4.4以上版本
*
* @param activity the activity
* @param colorId 颜色
*/
public static void setStatusBarColor(Activity activity, int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.setStatusBarColor(colorId);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使用SystemBarTintManager,需要先将状态栏设置为透明
setTranslucentStatus(activity);
SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
systemBarTintManager.setStatusBarTintEnabled(true);//显示状态栏
systemBarTintManager.setStatusBarTintColor(colorId);//设置状态栏颜色
}
}
/**
* 设置状态栏透明
*
* @param activity the activity
*/
@TargetApi(19)
public static void setTranslucentStatus(Activity activity) {
// 5.0以上系统状态栏透明
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) { //19
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 设置沉浸式状态栏
*
* @param activity the activity
* @param fontIconDark 状态栏字体和图标颜色是否为深色
*/
public static void setImmersiveStatusBar(Activity activity,boolean fontIconDark) {
setTranslucentStatus(activity);
if (fontIconDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setStatusBarFontIconDark(activity,TYPE_M);
} else if (OSUtils.isMiui()) {
setStatusBarFontIconDark(activity,TYPE_MIUI);
} else if (OSUtils.isFlyme()) {
setStatusBarFontIconDark(activity,TYPE_FLYME);
} else {//其他情况下我们将状态栏设置为灰色,就不会看不见字体
setStatusBarColor(activity,Color.LTGRAY);//灰色
}
}
}
/**
* 设置文字颜色
*
* @param activity the activity
* @param type the type
*/
public static void setStatusBarFontIconDark(Activity activity,@ViewType int type) {
switch (type) {
case TYPE_MIUI:
setMiuiUI(activity,true);
break;
case TYPE_M:
setCommonUI(activity);
break;
case TYPE_FLYME:
setFlymeUI(activity,true);
break;
}
}
/**
* Sets common ui.
*
* @param activity the activity
*/
//设置6.0的字体
public static void setCommonUI(Activity activity) {
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);
}
}
/**
* Sets flyme ui.
*
* @param activity the activity
* @param dark the dark
*/
//设置Flyme的字体
public static void setFlymeUI(Activity activity,boolean dark) {
try {
Window window = activity.getWindow();
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);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Sets miui ui.
*
* @param activity the activity
* @param dark the dark
*/
//设置MIUI字体
public static void setMiuiUI(Activity activity,boolean dark) {
try {
Window window = activity.getWindow();
Class clazz = activity.getWindow().getClass();
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
int 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);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.SystemBarTintManager
public class SystemBarTintManager {
/**
* The constant DEFAULT_TINT_COLOR.
*/
public static final int DEFAULT_TINT_COLOR = 0x99000000;
private boolean mStatusBarAvailable;
private boolean mStatusBarTintEnabled;
private View mStatusBarTintView;
/**
* Instantiates a new System bar tint manager.
*
* @param activity the activity
*/
@TargetApi(19)
public SystemBarTintManager(Activity activity) {
Window win = activity.getWindow();
//获取DecorView
ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 检查主题中是否有透明的状态栏
int[] attrs = {android.R.attr.windowTranslucentStatus};
TypedArray a = activity.obtainStyledAttributes(attrs);
try {
mStatusBarAvailable = a.getBoolean(0, false);
} finally {
a.recycle();
}
WindowManager.LayoutParams winParams = win.getAttributes();
int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;//状态栏透明
if ((winParams.flags & bits) != 0) {
mStatusBarAvailable = true;
}
}
if (mStatusBarAvailable) {
setupStatusBarView(activity, decorViewGroup);
}
}
/**
* 初始化状态栏
*
* @param context
* @param decorViewGroup
*/
private void setupStatusBarView(Activity context, ViewGroup decorViewGroup) {
mStatusBarTintView = new View(context);
//设置高度为Statusbar的高度
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context));
params.gravity = Gravity.TOP;
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
//默认不显示
mStatusBarTintView.setVisibility(View.GONE);
//decorView添加状态栏高度的View
decorViewGroup.addView(mStatusBarTintView);
}
/**
* 获取状态栏高度
*
* @param activity
* @return
*/
private int getStatusBarHeight(Activity activity) {
int statusBarHeight = 0;
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
/**
* 显示状态栏
*
* @param enabled the enabled
*/
public void setStatusBarTintEnabled(boolean enabled) {
mStatusBarTintEnabled = enabled;
if (mStatusBarAvailable) {
mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);
}
}
/**
* 设置状态栏颜色
*
* @param color the color
*/
public void setStatusBarTintColor(int color) {
if (mStatusBarAvailable) {
mStatusBarTintView.setBackgroundColor(color);
}
}
}
3.OSUtils
private static final String TAG = "Rom";
/**
* The constant ROM_MIUI.
*/
public static final String ROM_MIUI = "MIUI";
/**
* The constant ROM_EMUI.
*/
public static final String ROM_EMUI = "EMUI";
/**
* The constant ROM_FLYME.
*/
public static final String ROM_FLYME = "FLYME";
/**
* The constant ROM_OPPO.
*/
public static final String ROM_OPPO = "OPPO";
/**
* The constant ROM_SMARTISAN.
*/
public static final String ROM_SMARTISAN = "SMARTISAN";
/**
* The constant ROM_VIVO.
*/
public static final String ROM_VIVO = "VIVO";
/**
* The constant ROM_QIKU.
*/
public static final String ROM_QIKU = "QIKU";
private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";
private static final String KEY_VERSION_EMUI = "ro.build.version.emui";
private static final String KEY_VERSION_OPPO = "ro.build.version.opporom";
private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version";
private static final String KEY_VERSION_VIVO = "ro.vivo.os.version";
private static String sName;
private static String sVersion;
/**
* Is emui boolean.
*
* @return the boolean
*/
public static boolean isEmui() {
return check(ROM_EMUI);
}
/**
* Is miui boolean.
*
* @return the boolean
*/
public static boolean isMiui() {
return check(ROM_MIUI);
}
/**
* Is vivo boolean.
*
* @return the boolean
*/
public static boolean isVivo() {
return check(ROM_VIVO);
}
/**
* Is oppo boolean.
*
* @return the boolean
*/
public static boolean isOppo() {
return check(ROM_OPPO);
}
/**
* Is flyme boolean.
*
* @return the boolean
*/
public static boolean isFlyme() {
return check(ROM_FLYME);
}
/**
* Is 360 boolean.
*
* @return the boolean
*/
public static boolean is360() {
return check(ROM_QIKU) || check("360");
}
/**
* Is smartisan boolean.
*
* @return the boolean
*/
public static boolean isSmartisan() {
return check(ROM_SMARTISAN);
}
/**
* Gets name.
*
* @return the name
*/
public static String getName() {
if (sName == null) {
check("");
}
return sName;
}
/**
* Gets version.
*
* @return the version
*/
public static String getVersion() {
if (sVersion == null) {
check("");
}
return sVersion;
}
/**
* Check boolean.
*
* @param rom the rom
* @return the boolean
*/
public static boolean check(String rom) {
if (sName != null) {
return sName.equals(rom);
}
if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) {
sName = ROM_MIUI;
} else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) {
sName = ROM_EMUI;
} else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) {
sName = ROM_OPPO;
} else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) {
sName = ROM_VIVO;
} else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) {
sName = ROM_SMARTISAN;
} else {
sVersion = Build.DISPLAY;
if (sVersion.toUpperCase().contains(ROM_FLYME)) {
sName = ROM_FLYME;
} else {
sVersion = Build.UNKNOWN;
sName = Build.MANUFACTURER.toUpperCase();
}
}
return sName.equals(rom);
}
/**
* Gets prop.
*
* @param name the name
* @return the prop
*/
public static String getProp(String name) {
String line = null;
BufferedReader input = null;
try {
Process p = Runtime.getRuntime().exec("getprop " + name);
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
line = input.readLine();
input.close();
} catch (IOException ex) {
Log.e(TAG, "Unable to read prop " + name, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return line;
}
}
4.使用方法
StatusBarUtil.setTranslucentStatus(LoginActivity.this); StatusBarUtil.setStatusBarColor(this, Color.parseColor("#FAFAFA")); StatusBarUtil.setImmersiveStatusBar(this, false); StatusBarUtil.setStatusBarFontIconDark(this, StatusBarUtil.TYPE_M);