---
title: "ActionBarSherlock使用教程 ."
author: "Java-Object"
date: "2013-08-08T10:44:38Z"
tags: []
categories: ["编程语言"]
slug: "actionbarsherlock"
draft: false
lastmod: "2013-08-08T10:44:38Z"
id: 1921866
url: "https://hezd.iteye.com/blog/1921866"
---

Android3.0开始就提供了ActionBar，这让Android的用户体验强了好多，但是这个ActionBar在Android3.0以下是不支持的，谷歌也没推出什么解决方案，不过呢，没有什么是实现不了的，有github上有牛人放出了开源的ActionBarSherlock，可以在Android2.x上实现ActionBar，如果你在Android3.0的手机上使用，它会调用原生的ActionBar。

ActionBarSherlock在github上的地址是：[https://github.com/JakeWharton/ActionBarSherlock](https://github.com/JakeWharton/ActionBarSherlock)，它的官网是：[http://actionbarsherlock.com/](http://actionbarsherlock.com/)，下载即可，现在最新版本是4.2.0.

下载好，就解压出来，有三个文件夹，其实library就是Android公共库的使用，也就是将一些共同的东西抽取出来，包括java文件，资源文件等。

我的开发工具是eclipse，并安装了Android-maven，然后将这个library项目以maven项目导入即可。

接着新建一个Android工程，然后右键选择properties，再选择Android，接着添加library进来，如下：

[![actionbar1 ActionBarSherlock使用教程](http://www.chenwg.com/wp-content/uploads/2013/04/actionbar1.jpg "ActionBarSherlock使用教程")](http://www.chenwg.com/wp-content/uploads/2013/04/actionbar1.jpg)

1.修改项目的AndroidManifest.xml文件，将Android的Theme，也就主题改为如下：

android:theme=”@style/Theme.Sherlock.Light”  ，也就是使用ActionBarSherlock里的主题；

2.修改MainActivity.java文件，默认生成的MainActivity类是继承Activity的，现在将Activity修改成SherlockActivity；

3.修改menu文件夹下的main.xml文件如下：

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

`<menuxmlns:android="http://schemas.android.com/apk/res/android">`

 

`<item`

`android:id="@+id/add"`

`android:icon="@android:drawable/ic_menu_add"`

`android:showAsAction="ifRoom"`

`android:title="@string/add"`

`android:titleCondensed="@string/add">`

`</item>`

`<item`

`android:id="@+id/delete"`

`android:icon="@android:drawable/ic_menu_delete"`

`android:showAsAction="ifRoom"`

`android:title="@string/delete"`

`android:titleCondensed="@string/delete">`

`</item>`

 

`</menu>`

4.要使ActionBar显示menu按钮，得重写MainActivity中的onCreateOptionsMenu方法，还要导入ActionBarSherlock中的Menu、MenuItem包，如下：

1

2

3

4

5

6

7

`importcom.actionbarsherlock.view.Menu;`

`importcom.actionbarsherlock.view.MenuItem;`

`@Override`

`publicboolean` `onCreateOptionsMenu(Menu menu) {`

`getSupportMenuInflater().inflate(R.menu.main, menu);`

`returntrue;`

`}`

运行下模拟器，如下：

[![actionbar ActionBarSherlock使用教程](http://www.chenwg.com/wp-content/uploads/2013/04/actionbar.jpg "ActionBarSherlock使用教程")](http://www.chenwg.com/wp-content/uploads/2013/04/actionbar.jpg)

 

1.在项目中add library。方法是右击属性，然后再android类中，点击add library,选择我们刚刚导入的ActionBarSherlock库的jar

![](http://www.xue5.com/Mobile/UploadFiles_7297/201304/2013040618173119.jpg)

2.导入后，可能有些人会出现jar包冲突，这是因为abs和自己项目中同时存在android-support-v4这个包，这时只要删掉自己项目中的这个jar包就ok了。

至此，我们就能在项目中使用abs 的 actionbar了

3.修改AndroidManifest.xml,添加或修改代码,如下所示

 

 

**\[xml]** [view plain](http://blog.csdn.net/huanghuanghbc/article/details/8896814 "view plain")[copy](http://blog.csdn.net/huanghuanghbc/article/details/8896814 "copy")[print](http://blog.csdn.net/huanghuanghbc/article/details/8896814 "print")[?](http://blog.csdn.net/huanghuanghbc/article/details/8896814 "?")

1. &lt;uses-sdk
2. android:minSdkVersion="7"
3. android:targetSdkVersion="16" /&gt;
4. &lt;uses-feature
5. android:name="android.hardware.touchscreen"
6. android:required="false" /&gt;

```
<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="16" />

<uses-feature
    android:name="android.hardware.touchscreen"
    android:required="false" />
```

在application节增加以下内容   
android:theme="@style/Theme.Sherlock" 

**\[xml]** [view plain](http://blog.csdn.net/huanghuanghbc/article/details/8896814 "view plain")[copy](http://blog.csdn.net/huanghuanghbc/article/details/8896814 "copy")[print](http://blog.csdn.net/huanghuanghbc/article/details/8896814 "print")[?](http://blog.csdn.net/huanghuanghbc/article/details/8896814 "?")

1. &lt;application
2. android:icon="@drawable/ic\_launcher"
3. android:label="@string/app\_name"
4. android:theme="@style/Theme.Sherlock" &gt;

```
<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
```

设置在2.x下使用actionBar样式来显示界面,不然会显示为菜单样式

 

4.修改相应类  
相应类的对应关系  
android.app.Activity-&gt;com.actionbarsherlock.app.SherlockActivity  
android.app.Fragment-&gt;android.support.v4.app.Fragment  
android.app.FragmentTransaction-&gt;android.support.v4.app.FragmentTransaction  
android.view.Menu-&gt;com.actionbarsherlock.view.Menu  
android.view.MenuItem-&gt;com.actionbarsherlock.view.MenuItem

方法对应关系  
getFragmentManager-&gt;getSupportFragmentManager  
getMenuInflater-&gt;getSupportMenuInflater  
getActionBar-&gt;getSupportActionBar  
以上左边对应的为Activity的方法,右边对应的为SherlockActivity的方法

另外我在库中发现com.actionbarsherlock.app.SherlockFragmentActivity这个类,试了下和SherlockActivity差不多,某些情况下可以混合使用这两个类

5.把不需要的包从import导入列表中注释掉,至此,项目已经可以在android2.x-4.x中成功运行  
\----------  
actionbarsherlock库下载(abs4.2.0.jar,版本4.2.0):  
[http://www.kuaipan.cn/file/id\_10148578223724194.html](http://www.kuaipan.cn/file/id_10148578223724194.html)

 

转载自：[http://blog.csdn.net/huanghuanghbc/article/details/8896814](http://blog.csdn.net/huanghuanghbc/article/details/8896814)