错误:找不到与给定名称匹配的资源:属性'buttonBarButtonStyle'

17

我在styles.xml文件中编写了一个主题,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <!-- Root styles that vary by API level -->
<style name="FrameworkRoot.Theme" parent="Theme.Sherlock.Light.DarkActionBar">
    <!-- API 11+ (compatibility) -->
    <item name="buttonBarStyle">@style/Compat.ButtonBar</item>
    <item name="buttonBarButtonStyle">@style/Compat.ButtonBarButton</item>
    <item name="indeterminateProgressStyle">@style/Compat.IndeterminateProgress</item>
    <!-- API 14+ (compatibility) -->
    <item name="listPreferredItemPaddingLeft">@dimen/compat_list_preferred_item_padding_left</item>
    <item name="listPreferredItemPaddingRight">@dimen/compat_list_preferred_item_padding_right</item>
    <item name="listPreferredItemHeightSmall">@dimen/compat_list_preferred_item_height_small</item>
</style>

但是会出现错误:Error: No resource found that matches the given name: attr 'buttonBarStyle'。

我从Google IO2012中获取了代码。我的和它的都是基于相同的东西。

    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

我可以编译 Google IO2012 的源代码,但不能编译我的代码。太奇怪了。 有人知道原因吗?或者有同样的问题吗?


2
依我之见,你需要在../res/values文件夹中有compat.xml和attrs.xml。 - gsscoder
6个回答

18

我遇到了类似的问题。我的Eclipse工作区中有两个基于Android 4.2 jar的项目。一个项目的minSdkVersion="8",targetSdkVersion="17",其样式引用了buttonBarStylebuttonBarButtonStyle名称,运行正常。另一个项目使用相同的清单设置,但由于以下错误而无法构建:

 No resource found that matches the given name: attr 'buttonBarStyle'.
 No resource found that matches the given name: attr 'buttonBarButtonStyle'.

当我看到 @dead 的评论需要一个 attrs.xml 文件时,我检查了一下,果然无法编译的项目确实缺少了这个文件。 我想工作正常的项目是使用 Eclipse Android 应用程序项目向导生成的,而另一个是手动创建的。

res/values/attrs.xml 的内容:

<resources>

    <!--
         Declare custom theme attributes that allow changing which styles are
         used for button bars depending on the API level.
         ?android:attr/buttonBarStyle is new as of API 11 so this is
         necessary to support previous API levels.
    -->
    <declare-styleable name="ButtonBarContainerTheme">
        <attr name="buttonBarStyle" format="reference" />
        <attr name="buttonBarButtonStyle" format="reference" />
    </declare-styleable>

</resources>

res/values/colors.xml 的内容:

<resources>

    <color name="black_overlay">#66000000</color>

</resources>

还有三个样式文件:

1)res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--
        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="android:Theme.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.
        -->
    </style>

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

    <style name="ButtonBarButton" />

    <style name="ButtonBar">
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingRight">2dp</item>
        <item name="android:paddingBottom">0dp</item>
        <item name="android:background">@android:drawable/bottom_bar</item>
    </style>

    <style name="FullscreenTheme" parent="android:Theme.NoTitleBar">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@null</item> 
        <item name="buttonBarStyle">@style/ButtonBar</item>
        <item name="buttonBarButtonStyle">@style/ButtonBarButton</item>
    </style>

    <style name="ContentText">
        <item name="android:textColor">#0000ff</item>
        <item name="android:textSize">50sp</item>
        <item name="android:textStyle">bold</item>
    </style>

</resources>

2)res/values-v11/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
        <!-- API 11 theme customizations can go here. -->
    </style>

    <style name="FullscreenActionBarStyle" parent="android:Widget.Holo.ActionBar">
        <item name="android:background">@color/black_overlay</item>
    </style>

    <style name="FullscreenTheme" parent="android:Theme.Holo">
        <item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@null</item>
        <item name="buttonBarStyle">?android:attr/buttonBarStyle</item>
        <item name="buttonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
    </style>

</resources>

3) res/values-v14/styles.xml

<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>

</resources>

1
谢谢 - 这也让我更了解了Android中的主题。 - Ewald
非常好的答案。非常感谢。你救了我的命。 - Smeet

11

buttonBarStyle 是针对版本 11 及以上的,正如你在 SDK 中所述,但是你正在使用 android:minSdkVersion="8"。难道它不应该适用于 android:minSdkVersion="11" 吗?


当我将minSdkVersion="8"更改后,清理项目,错误仍然存在。 - jerry
3
顺便提一下,谷歌 I/O 2012 的代码可以在 android:minSdkVersion="8" 的版本上运行。我认为我可能漏掉了某些配置。 - jerry

1

0

我和你一样遇到了同样的问题:

我的 build.gradle 文件:

android {
compileSdkVersion 25
buildToolsVersion "27.0.1"
defaultConfig {
    applicationId "com.whatever"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}}

在我的Values文件夹中,我添加了一个attrs.XML文件,并在其中加入了以下代码:
<declare-styleable name="ButtonBarContainerTheme">
    <attr name="metaButtonBarStyle" format="reference" />
    <attr name="metaButtonBarButtonStyle" format="reference" />
</declare-styleable>

现在我看到没有任何抱怨!


0

0

我也遇到了同样的错误...在

C:\Users\.android\build-cache\Hash Number of Folders E.g. 4eaccf0d162ec009b115c7e0ff4f542f8d2a663b\

这个文件夹中删除了所有文件,问题就迎刃而解了...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接