用 SystemBarTintManager 实现很简单,就几行代码:
private void tintStatusBar(@NonNull Activity activity, @ColorInt int startColor, @ColorInt int endColor) {
Window window = activity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
GradientDrawable drawable =
new GradientDrawable(LEFT_RIGHT, new int[] { startColor, endColor });
final SystemBarTintManager tintManager = new SystemBarTintManager(this);
// enable status bar tint
tintManager.setStatusBarTintEnabled(true);
tintManager.setNavigationBarTintEnabled(false);
tintManager.setTintDrawable(drawable);
}