- What:浸入式状态栏作为一个非常优秀的设计方案受到大家的热捧,Android为了迎合趋势,也添加了浸入式状态栏。但是Android4.4系统是市场占有率使得我们必需在Android 4.4上面也要实现浸入式。
- How:在5.0系统上面实现浸入式比较简单,Material Design给我们提供了方案。
//设计状态栏的颜色
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
如果希望状态栏可以被图案填充,可以设置Activity的theme属性如下:
<style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
但是在4.4系统上面,以上代码就不行了,用了你会发现状态栏保持系统本身风格了,此时,你需要在values-v19(你们知道它是适配19也就是Android 4.4系统的哦)中添加styles文件,添加代码如下:
<style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
然后运行在Android 4.4的手机上你会发现为什么我的标题栏会向上移了,虽然状态栏浸入了,但是我的状态栏不对了,此时你需要将你的标题栏的宽度增加(加上状态栏的高度),以下介绍一个简单的实现方案:
//我将标题栏封装了一下,添加了一个paddingTop的属性
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/coffee"
android:orientation="vertical"
android:paddingTop="@dimen/statu_bar_height"
>
<TextView
android:id="@+id/titleBar"
android:layout_width="match_parent"
android:layout_height="@dimen/title_bar_height"
android:background="@color/coffee"/>
</LinearLayout>
上面的status_bar_height属性有什么用呢?
//values dimens中的定义
<dimen name="statu_bar_height">0dp</dimen>
//values-v19 dimens中的定义,(这个高度可以具体计算的,我就没算了,嘿嘿)
<dimen name="statu_bar_height">22dp</dimen>
运行一下之后发现在4.4跟5.0上面标题栏都是浸入式了。
3. Why:为什么会这样呢?我的理解是4.4系统的手机他只是将标题栏上移了而不是填充上去,所以会造成标题栏的位置不对了。
Tips:使用该方案的时候Layout中不能添加以下属性,否则4.4当中的状态栏就会变成一根白条:
android:fitsSystemWindows="true"
广告时间:大神带你飞,stay4it.com。报我名字给优惠哦!!!