定位API 28(Android 9)时,带有fitsSystemWindows的Android半透明状态栏始终为灰色

Sor*_*ner 6 android statusbar

从API 21开始,当样式包括<item name="android:windowTranslucentStatus">true</item>和布局包括在内时android:fitsSystemWindows="true",状态栏变为半透明,并且抽屉布局(如导航抽屉)在状态栏后面滑动。在定位API 28之前,状态栏的基本颜色将由colorPrimaryDark或设置android:statusBarColor。现在,这些值将被忽略。

这个问题实际上已经浮出水面com.android.support:design:27.1.0,但是在我当时认为它是一个bug并继续使用com.android.support:design:27.0.2。保留到API 28后,这似乎是未记录的设计更改。因此,fitsSystemWindows在API> = 28上使用时,如何设置状态栏的背景色?

Sor*_*ner 5

事实证明,此问题的答案是基本状态栏颜色现在由设置为的布局项目的背景设置fitsSystemWindows。然后通过半透明状态栏稀松布使该颜色变暗。因此,就我而言,fitsSystemWindows是在的CoordinatorLayout内部指定的DrawerLayoutandroid:background在上进行设置CoordinatorLayout可控制基本状态栏颜色。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

    <!-- `android:fitsSystemWindows="true"` moves `root_coordinatorlayout` below the system status bar.
     When it is specified, the theme should include `<item name="android:windowTranslucentStatus">true</item>`.
     `android:background` sets the background color of the status bar, which is then overlaid with a scrim. -->
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/root_coordinatorlayout"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.my.acivity"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:background="#FF64DD17" >

        <!-- The purpose of the `LinearLayout` is to place the included `main_webview` below `app_bar_layout`. -->
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical" >

            <!-- The `AppBarLayout` theme has to be defined here because the activity uses a `NoActionBar` theme. -->
            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:theme="@style/AppBarLight" >

                <android.support.v7.widget.Toolbar
                    android:id="@+id/app_bar"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent" />
            </android.support.design.widget.AppBarLayout>

            <!-- Include the main views. -->
            <include layout="@layout/main_views" />
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>

    <!-- The left drawer. -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationview"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/webview_navigation_menu"
        app:itemIconTint="?attr/navigationIconTintColor" />

    <!-- Include the right drawer. -->
    <include layout="@layout/right_drawer" />
Run Code Online (Sandbox Code Playgroud)

稀松布吸收#FF64DD17并使其变暗#FF3C850E

绿色状态栏

打开抽屉时,附加的稀松布会覆盖抽屉后面的整个应用程序,从而使状态栏进一步变暗#FF183405

导航抽屉打开