这几天翻了很多文章,试了好多次终于得出点结论了
最近一直在用Toolbar,所以就以Toolbar为准了
1.1在Android 4.4上设置沉浸式状态栏,需要在styles.xml中指定,
<item name="android:windowTranslucentStatus">true</item>
给布局文件
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="0dp" android:background="@color/colorRed" android:fitsSystemWindows="true"/>设置
android:fitsSystemWindows="true"
否则toolbar的内容就会陷进状态栏里
1.2在Android 5.0以上设置沉浸式状态栏,需要在代码中指定
if (Build.VERSION.SDK_INT >= 21) { View decorView = getWindow().getDecorView(); int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); getWindow().setStatusBarColor(Color.TRANSPARENT); }
这是 郭霖大神给出的一段代码,参考
https://blog.csdn.net/guolin_blog/article/details/51763825
用来将状态栏设置为透明,其实就是把它弄不存在了,这段代码只能在android 5.0及以上。
同样也需要指定,否则toolbar的内容就会陷进状态栏里
android:fitsSystemWindows="true"
说明:fitsSystemWindows是Toolbar里面的方法,参数是true或false
可以在代码中指定,也可以在xml中指定。
原理就是状态栏本身就是一个布局,通过两种方式让状态栏变透明,也就是状态栏的背景消失了,内容上移,不指定fitsSystemWindows就会相对应的上移以至于陷进状态栏,done。