简体   繁体   中英

AppCompat - Error while styling the actionbar

Im using the appcompat library to implement an action bar. While using a custom style it gives an error android:actionBarStyle requires API level 11 (current min is 10). I want the app to be compatible with 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).

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.

So you can duplicate your styles. Have a values/styles.xml and a 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.

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.

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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