使用 Theme.AppCompat 时操作栏颜色不会改变

Action bar color does not change when using Theme.AppCompat

我想要做的就是让操作栏和状态栏更改颜色以具有 material 设计。以下是我创建的主题:

 <!-- Base application theme. -->
<style name="AppTheme2" parent="Theme.AppCompat">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="android:colorPrimary">#FF5722</item>
    <!--<item name="android:statusBarColor">#E64A19</item>-->

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="android:colorPrimaryDark">#E64A19</item>


    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="android:colorAccent">#FF9800</item>

    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight & colorSwitchThumbNormal. -->

</style>

然而,当 运行 状态栏改变颜色时,操作栏仍然是黑色。我知道这个问题可能有一个简单的答案,但我错过了什么?

此问题与建议的问题不重复。请移除标记。

colorPrimary 中删除 android:appcompat-v7 没有在其主题中使用 android: 前缀,上次我检查过。

由于您使用的是 AppCompat,因此您应该使用 AppCompat 属性,而不是 Android 系统属性。

改变

android:colorPrimary

colorPrimary

整个事情应该是这样的:

<!-- Base application theme. -->
<style name="AppTheme2" parent="Theme.AppCompat">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#FF5722</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#E64A19</item>

    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#FF9800</item>
</style>