![a7dd8d4f2721c1f788332ef19ff42048.png](https://i-blog.csdnimg.cn/blog_migrate/b0edd2fb3fc5a0bca5ba8c9e048a4a9d.jpeg)
现在比较流行的就是浸入式,也就是状态栏颜色和当前页面颜色保持一致,为了有更好的视觉效果。
首先:
在setContentView()之前加上如下代码
tintManager.setStatusBarTintResource()可以设置想要的通知栏颜色
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(R.color.app_main_color);//通知栏所需颜色 } setContentView(R.layout.activity_base); } @TargetApi(19) private void setTranslucentStatus(boolean on) { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); }
效果图
![aa1d4173deb7847b682853482fff503b.png](https://i-blog.csdnimg.cn/blog_migrate/29882dbabe4d2afb76c1f7b9dd4500c2.jpeg)
SystemBarTintManager类附在下面
import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.Activity;import android.content.Context;import android.content.res.Configuration;import android.content.res.Resources;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.os.Build;import android.util.DisplayMetrics;import android.util.TypedValue;import android.view.Gravity;import android.view.View;import android.view.ViewConfiguration;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout.LayoutParams;import java.lang.reflect.Method;/** * Class to manage status and navigation bar tint effects when using KitKat * translucent system UI modes. * */public class SystemBarTintManager { static { // Android allows a system property to override the presence of the navigation bar. // Used by the emulator. // See https://github.com/android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#L1076 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { try { Class c = Class.forName("android.os.SystemProperties"); Method m = c.getDeclaredMethod("get