以前3.0以前的版本要使用ActionBar,必须使用国外大牛写的ActionBarSherlock这个开源项目。今年的Google/IO大会之后,Google官方在android-support-v7包中添加了ActionBar,开始让2.1以后的版本支持ActionBar,从此以后曾经最火的Android开源项目ActionBarSherlock可以退出历史舞台了。
要是用V7包中ActionBar也很简单,但有一个需要注意的地方。有些人可能刚开始仅仅是把android-support-v7-appcompat.jar导入项目中,但是在设置Activity的theme时会报错,提示找不到"@style/Theme.AppCompat"。这是由于我们要把v7和资源文件一起导入才行。
具体使用步骤(针对于Eclipse):
Create alibrary projectbased on the support library code:
-
Make sure you have downloaded theAndroid Support Libraryusing theSDK Manager.
-
Create a library project and ensure the required JAR files are included in the project's build path:
-
SelectFile > Import.
-
SelectExisting Android Code Into Workspaceand clickNext.
-
Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the
appcompat
project, browse to<sdk>/extras/android/support/v7/appcompat/
.
-
ClickFinishto import the project. For the v7 appcompat project, you should now see a new project titledandroid-support-v7-appcompat.
-
In the new library project, expand the
libs/
folder, right-click each.jar
file and selectBuild Path
> Add to Build Path. For example, when creating the the v7 appcompat project, add both theandroid-support-v4.jar
andandroid-support-v7-appcompat.jar
files
to the build path.
-
Right-click the project and selectBuild Path > Configure Build Path.
-
In theOrder and Exporttab, check the
.jar
files you just added to the build path, so they are available to projects that depend on this library project. For example,
theappcompat
project requires you to export both theandroid-support-v4.jar
andandroid-support-v7-appcompat.jar
files.
-
UncheckAndroid Dependencies.
-
ClickOKto complete the changes.
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:
-
In the Project Explorer, right-click your project and selectProperties.
-
In the Library pane, clickAdd.
-
Select the library project and clickOK. For example, the
appcompat
project should be listed asandroid-support-v7-appcompat.
-
In the properties window, clickOK.
Once your project is set up with the support library, here's how to add the action bar:
-
Create your activity by extending
ActionBarActivity
.
-
Use (or extend) one of the
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 theTheme.Holo
theme
(or one of its descendants), which is the default theme when either thetargetSdkVersion
orminSdkVersion
attribute
is set to"11"
or higher. If you don't want the action bar for an activity, set the activity theme toTheme.Holo.NoActionBar
.
以上摘自Android官网。
示例代码:
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.folyd.actionbartest"
-
android:versionCode="1"
-
android:versionName="1.0">
-
-
<uses-sdk
-
android:minSdkVersion="8"
-
android:targetSdkVersion="17"/>
-
-
<application
-
android:allowBackup="true"
-
android:icon="@drawable/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme">
-
<activity
-
android:theme="@style/Theme.Base.AppCompat.Light"
-
android:name="com.folyd.actionbartest.MainActivity"
-
android:label="@string/app_name">
-
<intent-filter>
-
<actionandroid:name="android.intent.action.MAIN"/>
-
-
<categoryandroid:name="android.intent.category.LAUNCHER"/>
-
</intent-filter>
-
</activity>
-
</application>
-
-
</manifest>
-
packagecom.folyd.actionbartest;
-
-
importandroid.os.Bundle;
-
importandroid.support.v7.app.ActionBar;
-
importandroid.support.v7.app.ActionBarActivity;
-
-
publicclassMainActivityextendsActionBarActivity{
-
privateActionBaractionBar;
-
-
@Override
-
protectedvoidonCreate(BundlesavedInstanceState){
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
actionBar=getSupportActionBar();
-
actionBar.setDisplayShowHomeEnabled(true);
-
}
-
-
}
效果截图:
分享到:
相关推荐
Google官方在android-support-v7包中添加了ActionBar,开始让2.1以后的版本支持ActionBar,此demo展现了它的简单使用!
使用Android-support-v7实现完全兼容Android 3.0以下系统,全面展示了怎么使用ActionBarActivity,绝对原创!
v7 appcompat 库中包含的一些关键类: ActionBar - 提供操作栏用户界面模式的实现。如需了解有关使用操作栏的详细信息,请参阅操作栏开发者指南。 AppCompatActivity - 添加一个应用 Activity 类,此类可作为使用...
低版本使用ActionBar时不仅要用到 v7jar包 还需要资源文件 要求我们必须把v7jar包以项目的形式导入。 又了这个文件一切可以解决
android-support-v7-appcompat源代码,从github上下载下来的。苦于用eclipse开发时使用ActionBar等组件不提示方法注释的亲们有福了!
带有传统标题栏 ActionBar 标题居中 的TitleActivity
android的actionbar开发,以及使用radio button代替Tab
Android 弹无虚发之第一弹:Android 2.X平台完美兼容ActionBar以及Actionbar的常用攻略 详细介绍请参考博客:http://blog.csdn.net/xy_nyle/article/details/17799753
android actionBar PPT
android-simple-actionbar-app 简单的Android ActionBar / Toolbar示例用法
工程需要通过actionBar去管理应用的头(Title),关联actionBar所在的库文件,android-support-v7-appcompat - 5.关联Xutils的库文件 - 6.运行项目前, 必须先启动WebServer, 保证可以从服务器获取数据
android ActionBar与Menu - 2 - email中ActionBar分析
Android SupportV7 ActionBar Demo
Beautiful gradient Actionbar for your Android-app ![img](https://raw.githubusercontent.com/flouthoc/Gradbar/master/gradbar.gif) This is a simple implementation of ui gradients on android Actionbar. ...
Android-使用Android-ActionBar 本教程的主要目的是解释Android ActionBar的工作原理。 您可以在此处找到有关如何使用代码库的完整教程: 本教程由The App Guruz提出-最好的
android-actionbar框架
ANDROID-添加ActionBar导航选项卡PART-2(带有GridView和MultipleSelection) ================== 这是ActionBar导航的示例代码。 更详细的检查这个 达瓦尔·索达·帕玛(Dhaval Sodha Parmar)
android-actionbar-for-2.x.x.7z