一、BaseActivity的onCreate方法里调用:setStatusBar(this, false, false);
二、设置沉浸式状态栏方法
/**
* 设置沉浸式状态栏
* useThemestatusBarColor 是否使用特殊的标题栏背景颜色,android5.0以上可以设置状态栏背景色,如果不使用则使用透明色值 (状态栏true白色,false透明色)
* withoutUseStatusBarColor 是否使用状态栏文字和图标为暗色,如果状态栏采用了白色系,则需要使状态栏和图标为暗色,android6.0以上可以设置(状态栏文字true浅色,false深色)
*/
public void setStatusBar(Activity activity, boolean useThemestatusBarColor, boolean withoutUseStatusBarColor) {
if (isFlyme()) {
CLog.e("状态栏------", "魅族");
processFlyMe(useThemestatusBarColor, activity);
} else if (isMIUI()) {
CLog.e("状态栏------", "小米");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
showStatusBarHeight();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
// DeviceUtil.processMIUI(false, activity);
} else if (Build.MANUFACTURER.equalsIgnoreCase("OPPO") && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
CLog.e("状态栏------", "OPPO");
setOPPOStatusTextColor(useThemestatusBarColor, activity);
} else {
CLog.e("状态栏------", "其他");
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);
}
showStatusBarHeight();
} 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);
showStatusBarHeight();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !withoutUseStatusBarColor) {
//android6.0以后可以对状态栏文字颜色和图标进行修改,设置状态栏文字颜色及图标为深色
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
}
/**
* 判断手机是否是魅族
*
* @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;
}
}
/**
* 改变魅族的状态栏字体为黑色,要求FlyMe4以上
*/
public 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();
}
}
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;
}
}
@TargetApi(19)
protected void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
private LinearLayout mLiStatesBar;//设置的顶部空状态栏位置
private int isShowStatusBarHeight = 0;//用来在外部获取实际状态栏高度
/**
* 动态设置状态栏高度
*/
protected void showStatusBarHeight() {
int statusHeight = ViewUtils.getStatusBarHeight(mContext);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLiStatesBar.getLayoutParams();
params.height = statusHeight;
mLiStatesBar.setLayoutParams(params);
isShowStatusBarHeight = 1;
}
/**
* 设置OPPO手机状态栏字体为黑色(colorOS3.0,6.0以下部分手机)
*
* @param lightStatusBar
* @param activity
*/
private static final int SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT = 0x00000010;
private static void setOPPOStatusTextColor(boolean lightStatusBar, Activity activity) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
int vis = window.getDecorView().getSystemUiVisibility();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (lightStatusBar) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (lightStatusBar) {
vis |= SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT;
} else {
vis &= ~SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT;
}
}
window.getDecorView().setSystemUiVisibility(vis);
}
/**
* 状态栏高度
* return int
*/
public int getStatusBarHeightint() {
int isStatusint = 0;
if (isShowStatusBarHeight == 1) {
isStatusint = ViewUtils.getStatusBarHeight(mContext);
}
return isStatusint;
}