简体   繁体   English

AppCompat-设置操作栏样式时出错

[英]AppCompat - Error while styling the actionbar

Im using the appcompat library to implement an action bar. 我正在使用appcompat库来实现操作栏。 While using a custom style it gives an error android:actionBarStyle requires API level 11 (current min is 10). 使用自定义样式时,它会给出错误android:actionBarStyle要求API级别11(当前最小值为10)。 I want the app to be compatible with android 2.3. 我希望该应用程序与android 2.3兼容。 Im using the google sample code. 我正在使用谷歌示例代码。

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>

AppCompat is compatible with 2.3, but you need to define 2 styles (as you have above): 1 for the native actionbar implementation (android:actionBarStyle) and one for the compatibility implementation (actionBarStyle). AppCompat与2.3兼容,但是您需要定义2种样式(如上所述):1种用于本机actionbar实现(android:actionBarStyle),一种是兼容性实现(actionBarStyle)。

If you define both those styles in your values/styles.xml, you will get compile errors - because values/styles.xml is compiled for all api versions, and 2.3 doesn't know about android:actionBarStyle. 如果在values / styles.xml中定义这两种样式,则会出现编译错误-因为values / styles.xml是针对所有api版本进行编译的,而2.3并不了解android:actionBarStyle。

So you can duplicate your styles. 因此,您可以复制样式。 Have a values/styles.xml and a values-v14/styles.xml 有一个values / styles.xml和一个values-v14 / styles.xml

The styles in values-v14/styles.xml will override any styles in values/styles.xml when run on a api 14 and above device. 当在api 14及更高版本的设备上运行时,values-v14 / styles.xml中的样式将覆盖values / styles.xml中的任何样式。

Or you can keep all your styles in the one values/styles.xml, but on the attributes you are getting the error on, add a tools:targetApi="14" to the tag, which tells the compiler to ignore this on non api 14 devices. 或者,您也可以将所有样式保留在一个values / styles.xml中,但是要在出现错误的属性上,将一个工具:targetApi =“ 14”添加到标记中,告诉编译器在非api上忽略它14个设备。

eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM