无法为 Android 个应用中的操作栏自定义 AppTheme

Unable to customize AppTheme for action bar in Android app

我正在尝试自定义操作栏。我以前也做过,但现在做不到了。不确定我哪里出错了。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar"
    parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/actionbar_gradient</item>
</style>

这是我要添加的可绘制对象

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="270"
        android:startColor="#d6ece542"
        android:endColor="#fffffdf4"
        />
</shape>

我还在我的清单中的 <application> 标签内添加了这个:

android:theme="@style/AppTheme"

但每次我都得到相同的操作栏(主题的默认设置)。谁能告诉我哪里出了问题?

另外仅供参考,我的 minSdk 版本=18,目标版本=21

您可能需要将 android:type="linear" 添加到该渐变定义中

<gradient
    android:angle="270"
    android:startColor="#d6ece542"
    android:endColor="#fffffdf4"
    android:type="linear"
    />

您似乎正在尝试将 Holo 操作栏主题应用到 Material AppCompat 主题。

一种解决方案是将渐变应用为 colorPrimary:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@drawable/actionbar_gradient</item>
</style>

更多自定义属性:https://developer.android.com/training/material/theme.html#StatusBar

另一个解决方案是切换到 Holo 主题:

  1. 将您的 activity 更改为 extends Activity
  2. 将您的父主题更改为 @android:style/Theme.Holo.Light.DarkActionBar