(colorPrimaryDark) 在安卓v21上无法改变状态栏颜色?

9

我在style.xml中添加了colorPrimaryDark,但在Android v21上状态栏没有变色。

我有一个自定义的工具栏,在style.xml代码中我使用了无操作栏主题。

如果有解决方案,请帮助我?

样式代码:

 <style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="myCustomToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColorSecondary">@color/colorAccent</item>
</style>

样式 v21:

  <style name="AppTheme" parent="AppTheme.Base">
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>

    <item name="windowActionBar">true</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

工具栏代码:

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:elevation="4dp"
android:background="@color/colorPrimary"
android:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"
xmlns:android="http://schemas.android.com/apk/res/android" />

主要布局代码
<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">

<LinearLayout
    android:fitsSystemWindows="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button1" />

</LinearLayout>


<android.support.design.widget.FloatingActionButton
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fab"
    android:layout_gravity="right|bottom"
    android:src="@mipmap/ic_plus"
    android:onClick="fabClick"
    android:layout_marginRight="@dimen/fab_margin"
    android:layout_marginBottom="@dimen/fab_margin_bottom"
    fab:borderWidth="0dp"/>

4个回答

15

我认为你的CoordinatorLayout缺少以下属性:android:fitsSystemWindows="true"

<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">

谢天谢地。你救了我的一天。拳头碰一下 - kopikaokao
1
我认为在进行一些布局更改时,我不小心删除了这个属性。过去几个小时一直在寻找解决方案。 - midhunhk

12

虽然时间有些久远,但我还是会回答的,你应该删除这行:

<item name="android:statusBarColor">@android:color/transparent</item>
它正在覆盖colorPrimaryDark颜色,基本上从状态栏中移除它。

它并没有覆盖。如果您添加android:statusBarColor,则不会覆盖colorPrimaryDark。 - Kishan Solanki
1
如果我想覆盖colorPrimaryDark,我该怎么做? - Andrey Solera

2
在values-v21文件夹中创建一个新的styles.xml文件,并在AppTheme中添加以下属性。
<item name="android:statusBarColor">@color/colorPrimaryDark</item>

我在styles.xml(v21)中使用了以下的AppTheme:
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>

1
虽然这个解决方案可行,但它会覆盖状态栏颜色。因此,如果您有导航抽屉或CollapsingToolbarLayout,则状态栏后面将是一个纯色条,而不是图像。 - CoolMind

-2

尝试使用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接