Android ActionBar后台colors.xml无法正常工作

时间:2014-06-04 16:11:49

标签: android xml android-actionbar android-actionbar-compat

我是android开发的初学者。我正在关注developer.android.com上的培训。但我现在正在改变动作栏背景颜色的问题。 我有错误

Description Resource    Path    Location    Type
error: Error: No resource found that matches the given name (at 'android:background' with value '@colors/orange').  styles.xml  /ActionBarColor/res/values  line 17 Android AAPT Problem

当我从res / values / styles.xml中引用res / values / colors.xml时

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name ="orange">#FF8800</color>
</resources>

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
        <item name="actionBarStyle">@style/myActionBar</item>
    </style>

    <style name = "myActionBar" parent = "Widget.AppCompat.Light.Base.ActionBar.Solid.Inverse">
        <item name ="android:background">@colors/orange</item> <!--Error Here-->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

我几乎阅读了每一个帖子,但没有发现任何线索。据我所知,我的代码看起来与其他代码完全相同。请帮我。项目目标是API19,min ver是API11

在网站上添加了themes.xml,错误仍然存​​在

<?xml version="1.0" encoding="utf-8"?>
<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">@colors/orange</item>

        <!-- Support library compatibility -->
        <item name="background">@colors/orange</item>
    </style>
</resources>

1 个答案:

答案 0 :(得分:2)

正如我所看到的,您正在Android项目中使用AppCompat库,因此您只应使用AppCompat资源进行主题化。有关如何Style the Action Bar的Android培训页面,请参阅"Customize the Background"部分:

<?xml version="1.0" encoding="utf-8"?>
<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>
</resources>

然后将您的主题应用于整个应用或个人活动:

<application android:theme="@style/CustomActionBarTheme" ... />
  1. 根据任何现有的AppCompat主题为操作栏创建样式,并在上面的示例中定义background项目(对于appcompat应用程序) - MyActionBar
  2. 创建一个样式,该项目将由您的项目使用,该项目应用以前创建的操作栏样式,并在上面的示例中定义actionBarStyle项目(对于appcompat应用程序) - CustomActionBarTheme
  3. 你真的想通读Android Training Pages,他们的解释很好;而且你也有Android API GuidesAndroid API Reference Pages以防万一。