Android AppCompat-设置actionbar样式时出错

Android AppCompat-设置actionbar样式时出错,android,android-layout,Android,Android Layout,我正在使用appcompat库实现一个操作栏。当使用自定义样式时,会出现一个错误android:actionBarStyle需要API级别11(当前最小值为10)。我希望该应用程序与android 2.3兼容。我正在使用谷歌的示例代码 themes.xml <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme"

我正在使用appcompat库实现一个操作栏。当使用自定义样式时,会出现一个错误android:actionBarStyle需要API级别11(当前最小值为10)。我希望该应用程序与android 2.3兼容。我正在使用谷歌的示例代码

themes.xml
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
       parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/actionbar_background</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_background</item>
</style>
themes.xml
@样式/MyActionBar
@样式/MyActionBar
@可绘制/操作栏\u背景
@可绘制/操作栏\u背景

AppCompat与2.3兼容,但您需要定义两种样式(如上所述):一种用于本机actionbar实现(android:actionBarStyle),另一种用于兼容实现(actionBarStyle)

如果在values/styles.xml中同时定义这两种样式,则会出现编译错误,因为values/styles.xml是针对所有api版本编译的,而2.3不了解android:actionBarStyle

所以你可以复制你的风格。 有一个values/styles.xml和一个values-v14/styles.xml

在api 14及以上设备上运行时,values-v14/styles.xml中的样式将覆盖values/styles.xml中的任何样式

或者,您可以将所有样式保留在一个values/styles.xml中,但对于要获取错误的属性,请在标记中添加一个tools:targetApi=“14”,这会告诉编译器在非api 14设备上忽略此选项

e、 g

@style/MyActionBar
<item name="android:actionBarStyle" tools:targetApi="14">@style/MyActionBar</item>