使用AppCompat Library进行ActionBar支持

时间:2013-12-06 19:46:10

标签: java android android-actionbar android-actionbar-compat android-appcompat

我需要在Android 2.x中实施操作栏我已尝试使用this发布并设置支持库,如this官方文章中所述。

以下是使用appcompat支持库实现ActionBar所遵循的步骤:

首先,我使用以下配置创建项目(我不创建图标和默认活动):

所需的最低SDK: API 10:Android 2.3.3(Gingerbread)
目标SDK: API 19:Android 4.4
编译: API 10:Android 2.3.3(Gingerbread)
主题:无

之后,在项目属性 =>中 Android => 图书馆我添加 android-support-v7-appcompat 库项目。

在此之后,我在使用 appcompat 库的项目中得到 128错误。所有这些都与 Holo 主题相关:

[2013-12-06 13:24:24 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v14\styles_base.xml:24: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Holo.ActionBar'.  

请注意,错误仅发生在 appcompat 库项目中的 values-v14 values-v11 文件夹中。以下是我得到的错误的两个例子:

[2013-12-06 13:24:24 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v14\styles_base.xml:100: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Holo.Widget.ActionBar.Subtitle.Inverse'.  
[2013-12-06 13:24:24 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v11\styles_base.xml:26: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Holo.ProgressBar.Horizontal'.  

此外,与 Holo 主题无关的错误很少:

[2013-12-06 13:24:38 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v14\styles_base.xml:225: error: Error: No resource found that matches the given name: attr 'android:dividerPadding'.  
[2013-12-06 13:24:38 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v14\styles_base.xml:224: error: Error: No resource found that matches the given name: attr 'android:showDividers'.  
[2013-12-06 13:24:38 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v11\themes_base.xml:33: error: Error: No resource found that matches the given name: attr 'android:windowActionBar'.  
[2013-12-06 13:24:38 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v11\themes_base.xml:44: error: Error: No resource found that matches the given name: attr 'android:windowActionBar'.  
[2013-12-06 13:24:38 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v14\themes_base.xml:41: error: Error: No resource found that matches the given name: attr 'android:actionBarWidgetTheme'.  
[2013-12-06 13:24:38 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v14\themes_base.xml:67: error: Error: No resource found that matches the given name: attr 'android:actionBarWidgetTheme'.  
[2013-12-06 13:24:38 - UsingSupportActionBar] C:\dev\Java\android\adt-bundle-windows-x86_64\sdk\extras\android\support\v7\appcompat\res\values-v14\themes_base.xml:94: error: Error: No resource found that matches the given name: attr 'android:actionBarWidgetTheme'.  

有没有人遇到同样的问题?

5 个答案:

答案 0 :(得分:15)

将“Compile with”设置为API级别19 - 当您使用SDK级别10编译应用程序时,编译器不知道错误日志中显示的元素。

答案 1 :(得分:2)

您需要将AppCompat中的res目录引用到您的项目中。在那里,您可以找到Support ActionBar所需的所有资源文件。

如何添加这些资源文件在google开发人员网站上有很好的描述:http://developer.android.com/tools/support-library/setup.html

向下滚动到:“使用资源添加库”。 按照说明,一切都应该没问题;)

答案 2 :(得分:0)

正如blackfizz所说,http://developer.android.com/tools/support-library/setup.html应该为您提供如何在Eclipse中设置库的良好方法。

另一方面注意:我强烈建议使用Android Studio IDE,因为它支持gradle构建系统,这将极大地帮助您设置不同的库。 如果您正在使用gradle,您只需添加:

dependencies {
    ...
    compile "com.android.support:appcompat-v7:18.0.+"
}

到您的build.gradle文件,gradle将为您完成剩下的工作。其他第三方库也可以非常容易地添加。它们将通过Maven Central自动解决。

几个星期前我已经用gradle切换到Android Studio IDE,我再也不想错过了。

答案 3 :(得分:0)

  1. 右键单击 - >财产 - >将标签切换为Android - >选择项目构建目标api级别16

  2. 在清单文件中添加 android:targetSdkVersion =" 16"

  3. 你的清单如下:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="android.support.v7.appcompat">
        <uses-sdk android:minSdkVersion="7"
            android:targetSdkVersion="16"/>
        <application />
    </manifest>
    
    1. 清理并构建库项目。
    2. 那是IT。

答案 4 :(得分:0)

转到项目的 Manifest.xml 文件。然后设置android:minSdkVersion =&#34; 11&#34;。 它会解决你的问题。迟到的回复但可能对其他人有帮助。