改變狀態欄顏色的彷彿只能用於小米,魅族,已經6.0以上的手機,vivo系統源碼不開發,所以沒辦法修改,直接上代碼
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
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 android.widget.LinearLayout;
import com.jaeger.library.StatusBarView;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by dell on 2016/10/26.
*/
public class BarTextColorUtils {
public static int StatusBarLightMode(Activity activity){
int result=0;
//這個方法只支持4.0以上系統
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if(MIUISetStatusBarLightMode(activity.getWindow(), true)){//判斷是不是小米系統
result=1;
}else if(FlymeSetStatusBarLightMode(activity.getWindow(), true)){//判斷是不是魅族系統
result=2;
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判斷當前是不是6.0以上的系統
activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
result=3;
}else {//如果以上都不符合就只能加個半透明的背景色了
result=4;
setTranslucentForCoordinatorLayout(activity, 70);
}
}
return result;
}
//帶有透明顏色的狀態欄
public static void setTranslucentForCoordinatorLayout(Activity activity, int statusBarAlpha) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
transparentStatusBar(activity);//先將狀態欄設置爲完全透明
addTranslucentView(activity, statusBarAlpha);//添加一個自定義透明度的矩形狀態欄
}
/**
* 使狀態欄透明
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private static void transparentStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 添加半透明矩形條
*
* @param activity 需要設置的 activity
* @param statusBarAlpha 透明值
*/
private static void addTranslucentView(Activity activity, int statusBarAlpha) {
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (contentView.getChildCount() > 1) {
contentView.getChildAt(1).setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));
} else {
contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
}
}
/**
* 創建半透明矩形 View
*
* @param alpha 透明值
* @return 半透明 View
*/
private static StatusBarView createTranslucentStatusBarView(Activity activity, int alpha) {
// 繪製一個和狀態欄一樣高的矩形
StatusBarView statusBarView = new StatusBarView(activity);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
return statusBarView;
}
/**
* 獲取狀態欄高度
*
* @param context context
* @return 狀態欄高度
*/
private static int getStatusBarHeight(Context context) {
// 獲得狀態欄高度
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}
/**
* 修改小米手機系統的
* @param window
* @param dark
* @return
*/
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;
}
/**
* 魅族手機修改該字體顏色
* @param window
* @param dark
* @return
*/
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;
}
}
小編髮現一個小問題,就是對於華爲有下部虛擬導航欄的手機來說說,可能會江虛擬導航欄變成白色或者透明四,最好最後把BarTextColorUtils中的activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);注掉試試
或者大家也可收索一下全局,能不能搜索到FLAG_TRANSLUCENT_NAVIGATION這個字段,不要把底部設置爲透明就可以了,由於我身邊沒有手機,所以這僅僅作爲提示