最近兼容到2.2甚至以下的版本,使用到了ActionBar,查看android官方解释看起来是个很简单的问题,不过本人走了点弯路,本文总结了一些遇到的问题与大家共勉。当然了你可以继续使用Android开源项目ActionBarSherlock,来实现兼容方式,不过既然Android官方有兼容包推出,那最好使用官方的嘛。首先把你的SDK升级到API-18,确认你的SDK中extras目录V7子目录有appcompat文件夹。
Android官网具体使用步骤(针对于Eclipse):
Create a library project based on the support library code:
appcompat
project, browse to <sdk>/extras/android/support/v7/appcompat/
.libs/
folder, right-click each .jar
file
and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar
andandroid-support-v7-appcompat.jar
files
to the build path..jar
files you just added to the build path, so they are available to projects that depend on this
library project. For example, the appcompat
project requires you to export both the android-support-v4.jar
and android-support-v7-appcompat.jar
files.You now have a library project for your selected Support Library that you can use with one or more application projects.
Add the library to your application project:
appcompat
project should be listed as android-support-v7-appcompat.Once your project is set up with the support library, here‘s how to add the action bar:
ActionBarActivity
.Theme.AppCompat
themes
for your activity. For example:<activity android:theme="@style/Theme.AppCompat.Light" ... >
Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher.
On API level 11 or higher
The action bar is included in all activities that use the Theme.Holo
theme
(or one of its descendants), which is the default theme when either the targetSdkVersion
or minSdkVersion
attribute
is set to "11"
or higher. If you don‘t want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar
.
官方解释就这么多,但是大家难免走一些弯路:
1、我直接将android-support-v4.jar android-support-v7-appcompat.jar,放入到我的目标工程不就完事了嘛?!还整一堆步骤那么麻烦,那你就错了,看起来编译没错,实际运行的时候你会越到如下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{jh.com.testandroidlayout/jh.com.testandroidlayout.navigator.NaviViewPagerActivity}: java.lang.IllegalStateException: You need to use
a Theme.AppCompat theme (or descendant) with this activity.
这个时候你仔细看看,发现原来继承自ActionBarActivity的类必须指定固定的集中Theme风格,而这些Theme风格是需要导入V7中的appcompat LIB库工程,编译后再引用才能引用使用。
黑色
android:theme="@style/Theme.AppCompat"
或浅色
android:theme="@style/Theme.AppCompat.Light"
但是我的应用工程在执行如下步骤后apply:
appcompat
project should be listed as android-support-v7-appcompat.
经过一番来回实验以后才发现原来需要将你的应用工程也要放在与android-support-v7-appcompat放在同一个workspace 中才可以。最后环境搭建成功,你可以尽情使用ActionBarActivity啦。
参考:
http://snailz.diandian.com/post/2014-01-22/40060824622
http://blog.csdn.net/hkg1pek/article/details/9473265
http://blog.csdn.net/appte/article/details/11712591
ActionBarActivity环境搭建步骤及遇到的问题总结,布布扣,bubuko.com
ActionBarActivity环境搭建步骤及遇到的问题总结
原文:http://blog.csdn.net/shuzui1985/article/details/20528305