状态栏正在将颜色更改为 'windowBackground' 而不是使用 'colorPrimaryDark'

Status bar is changing color to the 'windowBackground' instead of using 'colorPrimaryDark'

我正在使用 AppCompat 设计我的 android 移动应用程序。 运行 android 6.0 即 API 23.

这是我的 AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="FeedingTime.FeedingTime" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk />
    <application android:icon="@drawable/Icon" android:label="Feeding Time" android:theme="@style/AppTheme">
    </application>
</manifest>

这是我的 style.xml:

  <style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowTranslucentStatus">true</item>

    <item name="android:windowBackground">@color/background_color</item>

    <item name="colorPrimary">#6497b1</item>
    <item name="colorPrimaryDark">#005b96</item>
  </style>

这是 Activity 布局中存在问题的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
  <include
        android:id="@+id/toolbarHistoryActivity"
        layout="@layout/toolbar" />
  <ListView
      android:minWidth="25px"
      android:minHeight="25px"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="4"
      android:id="@+id/listViewHistory" />
  <Button
      android:text="Clear History"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:id="@+id/btnClearHistory" />
</LinearLayout>

我 运行 遇到了问题,在主屏幕中 activity 状态栏颜色按预期设置为深蓝色,但是当我打开第二个时 activity 我的状态条形图改变了颜色,它不再使用 'colorPrimaryDark',而是我在 'windowBackground' 中使用的颜色的深色版本。

这是为什么?

添加一个Coordinatorlayout作为root并使用android:fitsSystemWindows="true"

而不是Linearlayout

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:fitsSystemWindows="true">

您还可以在 Activity 中以编程方式设置 StatusBar 颜色:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.your_color));  //change color here
}