设置沉浸式状态栏很简单,但在这里要重视下,这里有一个坑。就是包裹着toolbar的最外层布局不能设置android:fitsSystemWindows="true",如果设置了,就会无效果。
还有直接给layout_height设置?actionBarSize。toolbar内的布局会网上移动,覆盖手机的状态栏上。
设置可分为三部分
一、去掉应用的ActionBar,可为activity设置theme为<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
也可以添加:<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
二、在toolbar布局中添加:android:minHeight="?actionBarSize"和android:fitsSystemWindows="true"。其中layout_height设置为wrap_content
fitsSystemWindows是ToolBar实现沉浸式状态栏的关键,其大概情况是,如果设为true,就会调整这个view去留一些空间给系统窗口,如果不设置或设为false,ToolBar就会和状态栏重叠在一起
三、在activity内也重新设置下,因为有些版本可能识别不了xml设置的android:fitsSystemWindows="true"
//手机顶部状态栏与toolbar颜色一致
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
WindowManager.LayoutParams barLayoutParams=getWindow().getAttributes();
barLayoutParams.flags=(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS|barLayoutParams.flags);
}
这就是我简单设置沉浸式状态栏的方法,和遇到的问题
还有一个坑,就是键盘弹出,toolbar被拉伸的问题;
网友评论