1、引入类库,直接在build.gradle文件中引入类库:
dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
}
2、在activity中添加方法
/** * Apply KitKat specific translucency. */ private void applyKitKatTranslucency() { // KitKat translucent navigation/status bar. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); SystemBarTintManager mTintManager = new SystemBarTintManager(this); mTintManager.setStatusBarTintEnabled(true); mTintManager.setStatusBarTintResource(R.color.title_bg);//通知栏所需颜色 } } @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); }在oncreate()方法中调用applyKitKatTranslucency()方法
3、在style.xml中添加系统的样式
<!-- 去掉tab顶部的黑边 -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> <!-- 沉浸式状态栏 --> <item name="android:fitsSystemWindows">true</item> <item name="android:clipToPadding">false</item> </style>别忘了在AndroidManifest.xml进行配置主题:
<application android:allowBackup="true" android:name=".App.App" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
解决使用沉浸式状态栏后toast不显示在中间的问题: