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

hra*_*ach 10 android material-design android-support-design

首先,我想提一下,有大量的问题和各种质量的答案,实际上我找不到一个好的解释,推理和解决问题的方法.

我想要:

  1. 一个主题 windowTranslucentStatus=true
  2. 有时在状态栏下"绘制"(例如DrawerLayout)
  3. 有时在状态栏下"不画画",让"组件/系统" colorPrimaryDark代替我画画.(可能android:fitsSystemWindows="true")

从各种资源中我已经意识到根布局之间存在差异,所以我所有的试验都是完全CoordinatorLayout适合的,并且已经为此做好了准备.(直接来自支持库,包含在所有样本中,包括工具栏,应用栏布局等)

我目前的设置:

编译api 27,支持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>
Run Code Online (Sandbox Code Playgroud)

价值观/ 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>
Run Code Online (Sandbox Code Playgroud)

值-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>
Run Code Online (Sandbox Code Playgroud)

结果如下:

状态栏没有颜色的工具栏


问题:

  1. 实际上甚至可以使用windowTranslucentStatus并且fitsSystemWindows没有"白/灰"状态栏吗?
  2. 似乎有一些支持在CoordinationLayout中绘制状态栏颜色时fitsSystemWindows,但(内部)属性(用于处理此属性)保持为null,如何解决这个问题?或者仅用于手动设置状态栏的颜色?
  3. 在哪种情况下android:statusBarColor考虑风格选项?哪个组件使用此值?为什么CoordinationLayout不选择此配置(使用其默认的primaryColorDark)并使用它?
  4. 是否支持其他任何布局?(相对,线性,约束)当它们具有这些(所述)缺陷时,它们是否真的被推荐为根布局?

笔记:

hra*_*ach 5

好的,我试着检查代码,发现这是解决方案!:好极了:

首先,我的问题的一些答案:

  1. 是的.应用程序(视图)负责绘制内容.DrawerLayout这是自动的,CoordinatorLayout也可以,见下.
  2. 有一个属性app:statusBarBackground可以直接在样式中的xml标签上设置!
  3. 对不起,我不知道(还是!:))
  4. 通过快速代码演练,似乎没有任何支持绘制状态栏.

所以解决方案是使用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>
Run Code Online (Sandbox Code Playgroud)