ImmersiveStatusBar修改状态栏背景色和图标颜色工
2017-11-18 本文已影响67人
隰有荷
首先直接~直接~直接拷贝ImmersiveStatusBar类,完全解耦,里面方法很多,但是常用的有三个,将在文章最后举例给出。
/**
本工具类作为http://www.jianshu.com/p/e14db8a794de文章中的小标题2:ImmersionBar类的一个补充工具使用,因ImmersionBar的使用,会使得输入法将布局顶起引起遮盖EditText的情况,故如遇此情况,使用本工具类(本方法也可以独立使用于6.0以上机型)。一般情况(大约九成以上)的状态栏问题依然建议使用ImmersionBar。
*/
别看方法多,二话不说直接从下方开始复制:
public class ImmersiveStatusBar {
private Activity mActivity;
public ImmersiveStatusBar(Activity activity) {
this.mActivity = activity;
}
/**
* 修改状态栏为全透明
* @param activity
*/
@TargetApi(19)
public static void transparencyBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* @param color 颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setColorBar(@ColorInt int color) {
setColorBar(color, 0);
}
/**
* @param color 颜色
* @param alpha 透明度
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setColorBar(@ColorInt int color, int alpha) {
setColorBar(color, alpha, false);
}
/**
* @param color 颜色
* @param isTransformNavigationBar 是否改变导航栏颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setColorBar(@ColorInt int color, boolean isTransformNavigationBar) {
setColorBar(color, 0, isTransformNavigationBar);
}
/**
* @param color 颜色
* @param alpha 透明度
* @param isTransformNavigationBar 是否改变导航栏颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setColorBar(@ColorInt int color, int alpha, boolean isTransformNavigationBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = mActivity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
int alphaColor = alpha == 0 ? color : calculateColor(color, alpha);
window.setStatusBarColor(alphaColor);
if (isTransformNavigationBar) {
window.setNavigationBarColor(alphaColor);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = mActivity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
int alphaColor = alpha == 0 ? color : calculateColor(color, alpha);
ViewGroup decorView = (ViewGroup) window.getDecorView();
decorView.addView(createStatusBarView(mActivity, alphaColor));
if (navigationBarExist(mActivity) && isTransformNavigationBar) {
decorView.addView(createNavigationBarView(mActivity, alphaColor));
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
setRootView(mActivity, true);
}
}
/**
* @param color 颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setColorBarForDrawer(@ColorInt int color) {
setColorBarForDrawer(color, 0);
}
/**
* @param color 颜色
* @param alpha 透明度
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setColorBarForDrawer(@ColorInt int color, int alpha) {
setColorBarForDrawer(color, alpha, false);
}
/**
* @param color 颜色
* @param isTransformNavigationBar 是否改变导航栏颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setColorBarForDrawer(@ColorInt int color, boolean isTransformNavigationBar) {
setColorBarForDrawer(color, 0, isTransformNavigationBar);
}
/**
* @param color 颜色
* @param alpha 透明度
* @param isTransformNavigationBar 是否改变导航栏颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setColorBarForDrawer(@ColorInt int color, int alpha, boolean isTransformNavigationBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = mActivity.getWindow();
ViewGroup decorView = (ViewGroup) window.getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
if (navigationBarExist(mActivity)) {
option = option | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
}
decorView.setSystemUiVisibility(option);
window.setStatusBarColor(Color.TRANSPARENT);
if (isTransformNavigationBar) {
window.setNavigationBarColor(Color.TRANSPARENT);
}
int alphaColor = alpha == 0 ? color : calculateColor(color, alpha);
decorView.addView(createStatusBarView(mActivity, alphaColor), 0);
if (navigationBarExist(mActivity) && isTransformNavigationBar) {
decorView.addView(createNavigationBarView(mActivity, alphaColor), 1);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = mActivity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
ViewGroup decorView = (ViewGroup) window.getDecorView();
int alphaColor = alpha == 0 ? color : calculateColor(color, alpha);
decorView.addView(createStatusBarView(mActivity, alphaColor), 0);
if (navigationBarExist(mActivity) && isTransformNavigationBar) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
decorView.addView(createNavigationBarView(mActivity, alphaColor), 1);
}
}
}
/**
* 设置状态栏文本和图标颜色(黑,白)
*
* @param isChangeColor 是否改变颜色 true:黑色 false:白色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setStatusTextAndIconColor(boolean isChangeColor) {
// 6.0以及6.0以上状态栏字体变色
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeColor) {
Window window = mActivity.getWindow();
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setImmersionBar() {
setTransparentBar(Color.TRANSPARENT);
}
/**
* @param color 颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setTransparentBar(@ColorInt int color) {
setTransparentBar(color, 0);
}
/**
* @param color 颜色
* @param alpha 透明度
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setTransparentBar(@ColorInt int color, int alpha) {
setTransparentBar(color, alpha, false);
}
/**
* @param color 颜色
* @param isTransformNavigationBar 是否改变导航栏颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setTransparentBar(@ColorInt int color, boolean isTransformNavigationBar) {
setTransparentBar(color, 0, isTransformNavigationBar);
}
/**
* @param color 颜色
* @param alpha 透明度
* @param isTransformNavigationBar 是否改变导航栏颜色
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setTransparentBar(@ColorInt int color, int alpha, boolean isTransformNavigationBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = mActivity.getWindow();
View decorView = window.getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
int finalColor = alpha == 0 ? Color.TRANSPARENT :
Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
window.setStatusBarColor(finalColor);
if (isTransformNavigationBar) {
window.setNavigationBarColor(finalColor);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = mActivity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
ViewGroup decorView = (ViewGroup) window.getDecorView();
int finalColor = alpha == 0 ? Color.TRANSPARENT :
Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
decorView.addView(createStatusBarView(mActivity, finalColor));
if (navigationBarExist(mActivity) && isTransformNavigationBar) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
decorView.addView(createNavigationBarView(mActivity, finalColor));
}
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setHintBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
View decorView = mActivity.getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
/**
* 创建状态栏视图
*
* @param context
* @param color
* @return
*/
private View createStatusBarView(Context context, @ColorInt int color) {
View mStatusBarTintView = new View(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context));
params.gravity = Gravity.TOP;
mStatusBarTintView.setLayoutParams(params);
mStatusBarTintView.setBackgroundColor(color);
return mStatusBarTintView;
}
/**
* 创建导航栏视图
*
* @param context
* @param color
* @return
*/
private View createNavigationBarView(Context context, @ColorInt int color) {
View mNavBarTintView = new View(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getNavigationHeight(context));
params.gravity = Gravity.BOTTOM;
mNavBarTintView.setLayoutParams(params);
mNavBarTintView.setBackgroundColor(color);
return mNavBarTintView;
}
/**
* 导航栏是否存在
*
* @param activity
* @return
*/
private boolean navigationBarExist(Activity activity) {
WindowManager windowManager = activity.getWindowManager();
Display d = windowManager.getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
d.getRealMetrics(realDisplayMetrics);
}
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
@ColorInt
private int calculateColor(@ColorInt int color, int alpha) {
float a = 1 - alpha / 255f;
int red = color >> 16 & 0xff;
int green = color >> 8 & 0xff;
int blue = color & 0xff;
red = (int) (red * a + 0.5);
green = (int) (green * a + 0.5);
blue = (int) (blue * a + 0.5);
return 0xff << 24 | red << 16 | green << 8 | blue;
}
/**
* 设置根视图
*
* @param activity
* @param fit
*/
private void setRootView(Activity activity, boolean fit) {
ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
for (int i = 0, count = parent.getChildCount(); i < count; i++) {
View childView = parent.getChildAt(i);
if (childView instanceof ViewGroup) {
childView.setFitsSystemWindows(fit);
((ViewGroup) childView).setClipToPadding(fit);
}
}
}
/**
* 获取状态栏的高度
*
* @param context
* @return
*/
private int getStatusBarHeight(Context context) {
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}
/**
* 获取到导航高度
*
* @param context
* @return
*/
public int getNavigationHeight(Context context) {
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}
}
复制到此结束。
使用举例,如下图的三个常用方法:
image.png
结束。