Today I’m going to show you five functions that work with the status bar. These five functions are independent and can be combined in any order you want. Support for Android 4.4.
As shown in the figure:
immersion
public static void setStatusBarTranslucent(Window window, boolean translucent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = window.getDecorView();
if (translucent) {
decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
return defaultInsets.replaceSystemWindowInsets(
defaultInsets.getSystemWindowInsetLeft(),
0, defaultInsets.getSystemWindowInsetRight(), defaultInsets.getSystemWindowInsetBottom()); }}); }else {
decorView.setOnApplyWindowInsetsListener(null);
}
ViewCompat.requestApplyInsets(decorView);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (translucent) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else{ window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } ViewCompat.requestApplyInsets(window.getDecorView()); }}Copy the code
Status bar coloring
public static void setStatusBarColor(final Window window, int color, boolean animated) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (animated) {
int curColor = window.getStatusBarColor();
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), curColor, color);
colorAnimation.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
@TargetApi(21)
@Override
public void onAnimationUpdate(ValueAnimator animator) { window.setStatusBarColor((Integer) animator.getAnimatedValue()); }}); colorAnimation.setDuration(200).setStartDelay(0);
colorAnimation.start();
} else{ window.setStatusBarColor(color); }}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
View statusBarView = decorViewGroup.findViewWithTag("custom_status_bar_tag");
if (statusBarView == null) {
statusBarView = new View(window.getContext());
statusBarView.setTag("custom_status_bar_tag");
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(window.getContext()));
params.gravity = Gravity.TOP;
statusBarView.setLayoutParams(params);
decorViewGroup.addView(statusBarView);
}
if (animated) {
Drawable drawable = statusBarView.getBackground();
int curColor = Integer.MAX_VALUE;
if(drawable ! =null && drawable instanceof ColorDrawable) {
ColorDrawable colorDrawable = (ColorDrawable) drawable;
curColor = colorDrawable.getColor();
}
if(curColor ! = Integer.MAX_VALUE) {final View barView = statusBarView;
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), curColor, color);
colorAnimation.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
barView.setBackground(newColorDrawable((Integer) animator.getAnimatedValue())); }}); colorAnimation.setDuration(200).setStartDelay(0);
colorAnimation.start();
} else {
statusBarView.setBackground(newColorDrawable(color)); }}else {
statusBarView.setBackground(newColorDrawable(color)); }}}Copy the code
Black status bar
public static void setStatusBarStyle(Window window, boolean dark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = window.getDecorView();
decorView.setSystemUiVisibility(
dark ? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : 0); }}Copy the code
Hide the status bar
public static void setStatusBarHidden(Window window, boolean hidden) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (hidden) {
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else{ window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); }}if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
final View statusBarView = decorViewGroup.findViewWithTag("custom_status_bar_tag");
if(statusBarView ! =null) {
boolean hiding = statusBarView.isClickable();
if (hiding == hidden) {
return;
}
if (hidden) {
statusBarView.setClickable(true);
ObjectAnimator animator = ObjectAnimator.ofFloat(statusBarView, "y", -getStatusBarHeight(window.getContext()));
animator.setDuration(200);
animator.setStartDelay(200);
animator.start();
} else {
statusBarView.setClickable(false);
ObjectAnimator animator = ObjectAnimator.ofFloat(statusBarView, "y".0);
animator.setDuration(200); animator.start(); }}}}Copy the code
Adjust Toolbar location
public static void appendStatusBarPadding(View view, int viewHeight) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if(view ! =null) {
int statusBarHeight = getStatusBarHeight(view.getContext());
view.setPadding(view.getPaddingLeft(), statusBarHeight, view.getPaddingRight(), view.getPaddingBottom());
if (viewHeight > 0) {
view.getLayoutParams().height = statusBarHeight + viewHeight;
} else{ view.getLayoutParams().height = viewHeight; }}}}public static void removeStatusBarPadding(View view, int viewHeight) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if(view ! =null) {
view.setPadding(view.getPaddingLeft(), 0, view.getPaddingRight(), view.getPaddingBottom()); view.getLayoutParams().height = viewHeight; }}}public static int getStatusBarHeight(Context context) {
int statusBarHeight = -1;
// Get the ID of the status_bar_HEIGHT resource
int resourceId = context.getResources().getIdentifier("status_bar_height"."dimen"."android");
if (resourceId > 0) {
// Obtain the size value of the response based on the resource ID
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
Copy the code
Use it in fragments
Sometimes there are several fragments in an Activity, and each fragment may have a different status bar style. How do you gracefully follow the fragment to switch the status bar style? As shown in the picture below?
To achieve this, you need to do some encapsulation, and here is an encapsulation solution that simply overwrites the following methods to return the desired effect.
protected BarStyle preferredStatusBarStyle(a);
protected boolean preferredStatusBarHidden(a);
protected int preferredStatusBarColor(a);
protected boolean preferredStatusBarColorAnimated(a);
Copy the code
The source code in this