随笔
修改状态栏背景色
1.添加调用方法(Activity , color);
initStatusBar(this, R.color.black);
方法拆分_1
public void initStatusBar(Activity activity, @ColorRes int backgroundColor) {
setWindowImmersive();
ViewGroup root = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
View layoutView = null;
boolean isContainLinearLayout = false;
if (root instanceof LinearLayout) {
layoutView = root;
isContainLinearLayout = true;
} else {
int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
layoutView = root.getChildAt(i);
if (layoutView instanceof LinearLayout) {
isContainLinearLayout = true;
break;
}
}
}
if (isContainLinearLayout && ((LinearLayout) layoutView).getOrientation() == LinearLayout.VERTICAL) {
if ((((LinearLayout) layoutView).findViewById(R.id.header_statu_bar)) != null) {
View statu_bar = ((LinearLayout) layoutView).findViewById(R.id.header_statu_bar);
statu_bar.setBackgroundColor(getResources().getColor(backgroundColor));
return;
}
View status_bar = new View(activity);
status_bar.setBackgroundColor(getResources().getColor(backgroundColor));
status_bar.setId(R.id.header_statu_bar);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.height = StatusBarUtil.getStatusBarHeight(this);
status_bar.setLayoutParams(lp);
if (Build.VERSION.SDK_INT >= 19) {
((LinearLayout) layoutView).addView(status_bar, 0);
}
}
}
方法拆分_2
protected void setWindowImmersive() {
StatusBarUtil.setWindowImmersive(getWindow());
setNavigationBar(StatusBarUtil.isLightOrDark(ResourcesCompat.getColor(getResources(), R.color.Primary_Background, null)));
StatusBarUtil.changeStatusBarTextColor(getColor(), this);
}
public static void setWindowImmersive(Window window) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(Color.TRANSPARENT);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
public void setNavigationBar(boolean islight) {
int systemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
int bgcolor = android.R.color.black;
if (islight) {
bgcolor = android.R.color.white;
systemUiVisibility |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
} else {
systemUiVisibility &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
}
getWindow().setNavigationBarColor(ResourcesCompat.getColor(getResources(), bgcolor, null));
getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
}
@Override
public Resources getResources() {
Resources res = super.getResources();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
Configuration config = res.getConfiguration();
//跟随系统
config.fontScale = FontScaleUtil.getFontScale(this);
res.updateConfiguration(config, res.getDisplayMetrics());
}
return res;
}
public static boolean isLightOrDark(int color) {
boolean flag = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
flag = Color.luminance(color) > 0.5;
} else {
flag = isLightColor(color);
}
return flag;
}
public static boolean isLightColor(int color) {
double darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
if (darkness < 0.5) {
return true; // It's a light color
} else {
return false; // It's a dark color
}
}
方法拆分_3
public static void changeStatusBarTextColor(int color,Activity activity) {
if (isLightOrDark(color)) {
setLightStatusBar(activity); //黑色
} else {
clearLightStatusBar(activity); //白色
}
}
public static void setLightStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = activity.getWindow().getDecorView().getSystemUiVisibility(); // get current flag
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // add LIGHT_STATUS_BAR to flag
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP&&Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// activity.getWindow().getDecorView().setSystemUiVisibility(
// View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
//
// activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().setStatusBarColor(activity.getResources().getColor(R.color.Primary_Navigation_Bar_Background));
}else{
Logger.e(TAG,"current system api level is"+Build.VERSION.SDK_INT);
}
}
public static void clearLightStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = activity.getWindow().getDecorView().getSystemUiVisibility(); // get current flag
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP&&Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// activity.getWindow().getDecorView().setSystemUiVisibility(
// View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
//
// activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().setStatusBarColor(activity.getResources().getColor(R.color.lxsk_grey3));
}else{
Logger.e(TAG,"current system api level is"+Build.VERSION.SDK_INT);
}
}
ids.xml 避免状态栏重复添加
<item name="header_statu_bar" type="id"/>
public static int getStatusBarHeight(Context context) {
if (context == null) {
context = BContext.context();
}
return getInternalDimensionSize(context, STATUS_BAR_HEIGHT_RES_NAME);
}
private static int getInternalDimensionSize(Context context, String key) {
if (context == null) {
Logger.e(TAG, "%s", "getInternalDimensionSize return -1 key=" + key + " context=null");
return -1;
}
Resources resources = context.getResources();
int result = -1;
int resourceId = resources.getIdentifier(key, "dimen", "android");
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId);
}
return result;
}
2.移除调用方法
public void removeStatusBar(Activity activity) {
if (status_bar == null) {
return;
}
ViewGroup root = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
View layoutView = null;
boolean isContainLinearLayout = false;
if (root instanceof LinearLayout) {
layoutView = root;
isContainLinearLayout = true;
} else {
int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
layoutView = root.getChildAt(i);
if (layoutView instanceof LinearLayout) {
isContainLinearLayout = true;
break;
}
}
}
if (isContainLinearLayout && ((LinearLayout) layoutView).getOrientation() == LinearLayout.VERTICAL) {
if (Build.VERSION.SDK_INT >= 19) {
((LinearLayout) layoutView).removeView(status_bar);
}
}
}
3.修改状态栏的图标颜色
public static void changeStatusBarTextColor(int color,Activity activity) {
if (isLightOrDark(color)) {
setLightStatusBar(activity); //黑色
} else {
clearLightStatusBar(activity); //白色
}
}
补充源码地址: github.com/BINBINXIAO/…