废话不多说直接上代码
在onCreate() 方法里面
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); attributes.flags |= flagTranslucentNavigation; window.setAttributes(attributes); getWindow().setStatusBarColor(Color.TRANSPARENT); } else { Window window = getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation; window.setAttributes(attributes); } }
这样沉浸式状态栏就搞定了
当然不要忘记styles里面改成NO
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
这样状态栏虽然OK了但是你会发现toobar与状态栏重叠了,这时咱们就需要在布局中添加:
android:fitsSystemWindows="true"
这样就完成沉浸式状态栏