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>

但得到错误:错误:找不到与给定名称匹配的资源:attr 'buttonBarStyle'。

我从谷歌 io2012 获得代码。我的和它的全部基地

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

我可以编译 google io2012 的源代码,但不是我的。这么奇怪。有人知道为什么吗?或者有同样的问题?

4

6 回答 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's有关需要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>
于 2013-02-12T22:38:52.073 回答
11

buttonBarStyle如您在 SDK 中所述,适用于版本 11 及更高版本,但您使用的是android:minSdkVersion="8". 它不应该工作android:minSdkVersion="11"吗?

于 2012-07-20T07:09:44.457 回答
1

请参考以下链接。添加代码中使用的值并删除其余的值。希望它可以帮助你。

https://android.googlesource.com/platform/frameworks/base/+/2888524e03896831f487e5dee63f18f1c33c0115/core/res/res/values/attrs.xml

于 2013-10-23T17:13:45.623 回答
0

确保您已包含 AppCompat 。这是一个图书馆项目。您需要在您的 android 项目中引用库项目。

https://developer.android.com/tools/support-library/setup.html

检查主题添加带有资源的库。

于 2014-04-18T01:15:36.367 回答
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>

现在我看不到任何抱怨!

于 2017-11-16T20:02:42.100 回答
0

我遇到了同样的错误....删除了文件夹

C:\Users\.android\build-cache\Hash 文件夹数,例如 4eaccf0d162ec009b115c7e0ff4f542f8d2a663b\

它就像一个魅力......

于 2017-11-18T07:36:58.140 回答