简体   繁体   English

使用AppCompat Library进行ActionBar支持

[英]Use AppCompat Library for ActionBar support

I need to implement the actionbar in Android 2.x I've tried with this post and setup the support library as mentioned in this official article. 我需要在Android 2.x中实现操作栏我已经尝试过这篇文章并设置了这篇官方文章中提到的支持库。

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) 最低要求的SDK: API 10:Android 2.3.3(Gingerbread)
Target SDK: API 19: Android 4.4 目标SDK: API 19:Android 4.4
Compile with: API 10: Android 2.3.3 (Gingerbread) 编译: API 10:Android 2.3.3(姜饼)
Theme : None 主题 :无

After, in the Project properties => Android => Library I add the android-support-v7-appcompat library project. 之后,在Project properties => Android => Library中添加了android-support-v7-appcompat库项目。

After this, I get 128 errors like this in the project where I use the appcompat library. 在此之后,我在使用appcompat库的项目中得到了128个这样的错误 All of them are related with the Holo theme: 所有这些都与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-v14 and values-v11 folders in the appcompat library project. 请注意,错误仅发生在appcompat库项目中的values-v14values-v11文件夹中。 Here are 2 examples of the errors I get: 以下是我得到的两个错误示例:

[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 Holo theme: 此外,与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? 有没有人有同样的问题?

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

You need to reference the res directory from AppCompat to your project. 您需要将AppCompat中的res目录引用到您的项目。 There you can find all resource files, which are needed for the Support ActionBar. 在那里,您可以找到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 如何添加这些资源文件在google开发者网站上有很好的描述: http//developer.android.com/tools/support-library/setup.html

Scroll down to :"Adding libraries with resources". 向下滚动到:“使用资源添加库”。 Follow the instructions and everything should be fine ;) 按照说明,一切都应该没问题;)

As blackfizz said, http://developer.android.com/tools/support-library/setup.html should 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. 另一方面,我强烈建议使用Android Studio IDE,因为它支持gradle构建系统,这将极大地帮助您设置不同的库。 If you're using gradle, all you have to do is add: 如果你正在使用gradle,你所要做的就是添加:

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

to your build.gradle file and gradle will do the rest for you. 你的build.gradle文件和gradle将为你完成剩下的工作。 Other third party libraries can also be added super easily. 其他第三方库也可以非常容易地添加。 They'll automatically be resolved through Maven Central. 它们将通过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,我再也不想错过了。

  1. Right click -> Property -> Switch tab to Android -> Select project build target api level 16 右键单击 - >属性 - >切换选项卡到Android - >选择项目构建目标api级别16

  2. Add android:targetSdkVersion="16" in manifest file. 在清单文件中添加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. 清理并构建库项目。

That's IT. 而已。

Go to Manifest.xml file of your project. 转到项目的Manifest.xml文件。 Then set android:minSdkVersion="11". 然后设置android:minSdkVersion =“11”。 It will solve your problem. 它会解决你的问题。 Late reply but may be helpful for others. 迟到的回复但可能对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM