看着这标题有点纠结啊,明明一个问题咋两个名字呢,主要是最开始把这个状态栏的颜色和bar或者图片融合在一起,所以被广流传为沉浸状态栏,但这时网上理论学的好的大神们不乐意了,觉得这个定义完全就是错误的定义,意思都不对,也给了各种有利的证据。但这里我是和那些理论学的好的大师学的这个状态栏,但大神们都没给出统一的正确的定义,为了尊重大神,我自己给了同步状态栏这名。别纠结名字了,技术才最重要,现在步入正题。
首先还是要将一下这个fitsystemwindows这属性,具体意思不讲,主要是4.4以后出现的,所以4.4之前的都不行。所以我这里要讲的是4.4以上的适配,如果你想适配以下的,请另寻他法。
开始工作:分别创建三个style文件:values,values-v19,values-v21。如下图:
values里的styles:
values-19里的styles:
values-21里的styles:
好,为了适配请注意我的布局文件里的fitsystemwindows属性的设置
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_index"
android:fitsSystemWindows="true"
android:background="@color/wuan_blue"
tools:context=".IndexActivity">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_index"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/wuan_blue"
android:fitsSystemWindows="true"
>
以上主要默认适配5.0以上的,也是标准的用法
但如果是5.0以下的,你会发现无效,状态栏依旧没任何改变,这是令人特别头疼的问题,自己也从网上找了许多方法,但都是涉及系统方面的,自己也看不懂,头疼了几个星期,要么Ctrl C,V,要么只适配一个,但今天静下来花了点时间仔细研究,总算得到了一种更简洁易懂的方法,如想知到更多方法,点击这里。
我的方法主要就几行代码,在代码做个判断,如果小于5.0的话,就设fitsystemwindows为false:
if (Build.VERSION.SDK_INT <= 21) {
drawerIndex.setFitsSystemWindows(false);
coor.setFitsSystemWindows(false);
}
好了,适配完成,注意toolbar的高度必须为warp,否则又是个头疼的问题。最后进行最重要的一个,在manifest里引用style
最后附上图:
这是4.4的,这里不贴5.0以上的,不用脑子想都知道,肯定适配5.0以上的。
是不是特别简单!!!