Android 沉浸栏实践——踩坑

当前开发环境:Android Studio 2.1.3,compileSdkVersion 24,buildToolsVersion "24.0.2",support:appcompat-v7:24.2.0

首先放个图,这就是我要做成的效果,Toolbar 和 Status Bar 一体共用背景图,实际上就是 Toolbar 的背景图延伸到 Status Bar。

效果图

先做一点点思考。我不打算修改 toolbar 的高度,设置为 android:layout_height="?attr/actionBarSize" 就好,否则 fitsSystemWindows 之后就需要设置 toolbar 高度为 25dp + 48dp = 73dp,同时其他的内容也会改变。那么,可以考虑在 AppBarLayout 中设置背景,然后让它侵入到状态栏去。现在 style.xml 看起来会是这样:

<!-- values/style.xml --->
<resources>

    <style name="AppTheme" parent="@style/BaseAppTheme"/>

    <!-- Base application theme. -->
    <style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>

<!-- values-v19/style.xml --->
<resources>

    <style name="AppTheme" parent="@style/BaseAppTheme">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

</resources>

<!-- values-v21/style.xml --->
<resources>

    <style name="AppTheme" parent="@style/BaseAppTheme">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

    </style>
</resources>

toolbar 的布局文件差不多是这样子:

<!-- layout/toolbar.xml --->
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:paddingTop="@dimen/appbar_top_padding"
    android:background="@drawable/bg_bar"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

主布局文件差不多是这样子:

<!-- layout/activity_main.xml --->
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout 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:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.iamwent.tinter.MainActivity">

    <include layout="@layout/toolbar" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

        <TextView
            android:id="@+id/tv_sdk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"/>
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

然后你运行,发现 v21 的效果出来了,但是 v19 是这么个鬼!


v19效果

那个灰色条是什么?又是怎么来的呢?给跟布局设置背景色就可以发现,灰色条实际上是因为根布局侵入了状态栏,从我们给根布局设置的 android:fitsSystemWindows="true" 就是让布局上去

对于 android:windowTranslucentStatus文档上是这么说的:

By enabling translucent system bars, your layout will fill the area behind the system bars, so you must also enable fitsSystemWindows
for the portion of your layout that should not be covered by the system bars.
开启透明状态栏后,你的布局会填充状态栏下面的区域,所以你应当同时设置布局 fitsSystemWindows 以防止被状态栏覆盖。

而对于 android:fitsSystemWindows 文档又是这么说的:

Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.
配置 fitsSystemWindows 后,系统就会调整 view 的 padding 以给 system windows 留出空间。

解决办法是根布局不设置 android:fitsSystemWindows,然后在代码中判断,在 v21 以上手动设置这一属性,代码如下:

private void setTranslucentStatusBar() {
    int sdkInt = Build.VERSION.SDK_INT;

    if (sdkInt >= Build.VERSION_CODES.LOLLIPOP) {
        setTranslucentStatusBarLollipop();
    }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setTranslucentStatusBarLollipop() {
    ((ViewGroup) getWindow().findViewById(android.R.id.content)).getChildAt(0).setFitsSystemWindows(true);
}

昨晚吐槽的时候,有位朋友提醒了我,这个值也可以用 style 的方式设置:

<!-- values/style.xml --->
<style name="AppTheme.FitsSystemWindows">
</style>

<!-- values-v21/style.xml --->
<style name="AppTheme.FitsSystemWindows">
    <item name="android:fitsSystemWindows">true</item>
</style>

我脑袋一抽,觉得以下这个方式可能也可以:

<!-- values/style.xml --->
<bool name="fit_system_status_bar">false</bool>

<!-- values-v21/style.xml --->
<bool name="fit_system_status_bar">true</bool>

坑位零

在上面的配置中涉及到了多个属性的设置,建议修改为不同的值,看看它们会造成什么样的效果。

坑位一

奇怪的间隔

眼尖的人可能在最前面的效果图就看到了,那就是在 v23 上这个显示系统版本的 TextView 有一个奇怪的 margin,而且很巧合的就是 25dp!最后我发现,是由于给主布局的 RelativeLayout 设置了 app:layout_behavior="@string/appbar_scrolling_view_behavior" 造成的。解决办法是在 v23 上给 RelativeLayout 设置 android:layout_marginTop="-25dp",可以在 dimens 中做。

坑位二

在修改的过程中我还发现一个奇怪的问题,status bar 是个灰色的条,超级奇怪!最后对比发现是 AppTheme.NoActionBar 采用继承 Theme.AppCompat.Light.NoActionBar 的方式,解决办法是不继承 parent,采用手动配置。

Theme 造成的怪现象

结语

我是要给一个半成品的 APP 做适配,所以先单独做了一个 demo 实现想要的——后来踩到的坑证明我这个决定是多么的正确!特别是坑位三。所以一旦出现莫名其妙的错误,我就对照 demo 的配置一个个地方去排除。
另外,多试试各种配置的作用,明白它们影响的是什么区域,碰到问题才好修改。

Demo 在这里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,571评论 25 707
  • 前言 原文:http://blog.csdn.net/mybeta/article/details/5076032...
    naturs阅读 22,988评论 8 70
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,088评论 2 44
  • 花了两天时间把钱钟书的《围城》看完后,发现从方鸿渐中看到了自己的影子,太像了...... “爱情就像一座城,围在城...
    听风呤阅读 215评论 0 0
  • 最近往北京上海跑的勤了,心思更不在眼前的局面上了。 期待在新的工作里能够“洗心革面,重新做人”。 也有担心,是否会...
    司牧尧阅读 613评论 0 1