当半透明状态设置为 true 时,状态栏颜色保持无颜色

Status bar color stays without color when translucent status set to true

首先,我想提一下,有大量的问题,答案质量参差不齐,实际上我找不到很好的解释、推理和解决问题的方法。

我要:

  1. 主题 windowTranslucentStatus=true
  2. 有时"draw"在状态栏下方(例如DrawerLayout
  3. 有时"not draw"在状态栏下让"components/system"画colorPrimaryDark而不是我。 (可能 android:fitsSystemWindows="true"

从各种资源中,我意识到根布局之间存在差异,因此我的所有试验都是使用 CoordinatorLayout 完成的,这似乎是最合适的,也是为此做好准备的。 (直接来自支持库,包含在所有带有工具栏、应用栏布局等的示例中)

我当前的设置:

编译api27,支持的libs是版本27*

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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

    <fragment
        android:id="@+id/fragment"
        android:name="com.sygic.travel.materialtest5.MainActivityFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

values/styles.xml:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
</resources>

values-v21/styles.xml:

<resources>
    <style name="AppTheme" 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">@color/colorPrimaryDark</item>
    </style>
</resources>

这导致:


问题:

  1. 是否真的可以使用 windowTranslucentStatusfitsSystemWindows 而没有 "white/gray" 状态栏?
  2. 似乎在 fitsSystemWindows 时在 CoordinationLayout 中支持绘制状态栏颜色,但(内部)属性(用于处理此问题)仍然为 null,如何解决此问题?还是只能手动设置状态栏的颜色?
  3. 在什么情况下会考虑样式选项 android:statusBarColor?哪个组件使用这个值?为什么 CoordinationLayout 不选择此配置(使用其默认的 primaryColorDark)并使用它?
  4. 是否以某种方式支持任何其他布局? (Relative, Linear, Constraints)当它们有这些(所说的)缺陷时,它们是否真的被推荐为根布局?

备注:

好的,我尝试检查代码并发现这是解决方案! :是的:

首先,回答我的问题:

  1. 是的,是的。应用程序(视图)有责任绘制一些东西。 DrawerLayout 会自动执行此操作,CoordinatorLayout 也可以,请参阅下一步。
  2. 有一个属性 app:statusBarBackground可以直接在样式的xml标签上设置!
  3. 抱歉,我不知道(还不知道!:))
  4. 通过快速代码 walk-through 似乎不支持绘制状态栏。

所以解决方案是使用CoordinatorLayoutstatusBarBackground 属性:

values-v21/styles.xml

<resources>
    <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">@color/colorPrimaryDark</item>
    <item name="statusBarBackground">@color/colorPrimary</item>
</resources>