正文
今天为大家介绍 5 个处理状态栏的函数,这 5 个函数互不依赖,可以自由以任意次序组合以达成自己的需求。支持到 Android 4.4 。
如图所示:
沉浸式
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());
}
}
状态栏着色
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(new ColorDrawable((Integer) animator.getAnimatedValue()));
}
});
colorAnimation.setDuration(200 ).setStartDelay(0 );
colorAnimation.start();
} else {
statusBarView.setBackground(new ColorDrawable(color));
}
} else {
statusBarView.setBackground(new ColorDrawable(color));
}
}
}
黑色状态栏
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 );
}
}
隐藏状态栏
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();
}
}
}
}
调整 Toolbar 位置
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