Java 使用 AppCompat 库来支持 ActionBar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20432314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:52:52  来源:igfitidea点击:

Use AppCompat Library for ActionBar support

javaandroidandroid-actionbarandroid-actionbar-compatandroid-appcompat

提问by Jorge E. Hernández

I need to implement the actionbar in Android 2.x I've tried with thispost and setup the support library as mentioned in thisofficial article.

我需要在 Android 2.x 中实现 actionbar 我已经在这篇文章中尝试过并设置了这篇官方文章中提到的支持库。

Here are the steps that I've followed for implement the ActionBar using the appcompat support library:

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

First, I create the project with the following configuration (I don't create an icon and a default activity):

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

Minimum Required SDK:API 10: Android 2.3.3 (Gingerbread)
Target SDK:API 19: Android 4.4
Compile with:API 10: Android 2.3.3 (Gingerbread)
Theme: None

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

After, in the Project properties=> Android=> LibraryI add the android-support-v7-appcompatlibrary project.

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

After this, I get 128 errorslike this in the project where I use the appcompatlibrary. All of them are related with the Holotheme:

在此之后,我在使用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'.  

Please, note that the error only occurs with the values-v14and values-v11folders in the appcompatlibrary project. Here are 2 examples of the errors I get:

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

[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'.  

Also, there are few error not related with the Holotheme:

此外,很少有与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'.  

Has anyone had the same problem?

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

采纳答案by Julia Hexen

Set "Compile with" to API Level 19 as well - when you compile your app with SDK level 10, the compiler doesn't know the elements shown in the error log.

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

回答by blackfizz

You need to reference the res directory from AppCompat to your project. There you can find all resource files, which are needed for the Support ActionBar.

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

How to add those resource files is described pretty good on the google developer site: http://developer.android.com/tools/support-library/setup.html

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

Scroll down to :"Adding libraries with resources". Follow the instructions and everything should be fine ;)

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

回答by MrMaffen

As blackfizz said, http://developer.android.com/tools/support-library/setup.htmlshould give you a good how-to on how to setup the library in Eclipse.

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

On another note though: I can highly recommend using Android Studio IDE, as it supports the gradle build system which will enormously help with your struggle with setting up the different libraries. If you're using gradle, all you have to do is add:

但要注意的是:我强烈推荐使用 Android Studio IDE,因为它支持 gradle 构建系统,这将极大地帮助您解决设置不同库的困难。如果您使用的是 gradle,您所要做的就是添加:

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

to your build.gradle file and gradle will do the rest for you. Other third party libraries can also be added super easily. They'll automatically be resolved through Maven Central.

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

I've switched over to Android Studio IDE with gradle a few weeks ago and I never want to miss it again.

几周前我已经切换到带有 gradle 的 Android Studio IDE,我再也不想错过它了。

回答by Hiren Patel

  1. Right click -> Property -> Switch tab to Android -> Select project build target api level 16

  2. Add android:targetSdkVersion="16"in manifest file.

  1. 右键单击 -> 属性 -> 切换选项卡到 Android -> 选择项目构建目标 api 级别 16

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

Your manifest looks like this:

您的清单如下所示:

<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. Clean and build library project.
  1. 清理并构建库项目。

That's IT.

就是这样。

回答by Rameshbabu

Go to Manifest.xmlfile of your project. Then set android:minSdkVersion="11". It will solve your problem. Late reply but may be helpful for others.

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