From:http://blog.csdn.net/tianjf0514/article/details/13278539
Android3.0才开始支持ActionBar,那么怎么在低版本也能用ActionBar呢?
1、使用Google提供的Support Library(android-support-v7-appcompat)。
2、使用Jake Wharton编写的ActionBarSherlock(http://actionbarsherlock.com/)。
以上两个library的共同点:Android3.0以下,使用library,Android3.0以上,使用系统自带的ActionBar
以上两个library的不同点:android-support-v7-appcompat在Android3.0以下不支持Overflow按钮,如果要使用Overflow按钮,可以使用library里面提供的PopupMenu。从ActionBarSherlock v4.2.0开始在Android3.0以下不支持Overflow按钮,如果要使用Overflow按钮,可以使用ActionBarSherlock v4.1.0,但是新版本有修正的一些bug。
由于两个library的实装方法一样,那么下面就只介绍android-support-v7-appcompat的使用。
1、下载最新的SDK,然后找到里面的sdk\extras\android\support\v7\appcompat导入到eclipse里面。
2、右键里的项目 - Properties - Android - Add - 选择导入的appcompat项目,这样就可以使自己的项目引用appcompat项目。
3、导入之后如果在控制台提示有两个android-support-v4.jar,原因是自己项目中的android-support-v4.jar和导入的appcompat项目的android-support-v4.jar重复了,删掉自己项目中的android-support-v4.jar就OK了。
- Found 2 versions of android-support-v4.jar in the dependency list,
- but not all the versions are identical (check is based on SHA-1 only at this time).
- All versions of the libraries must be the same at this time.
- Versions found are:
- Path: D:\Program Files\adt-bundle-windows-x86_64-20130917\eclipse\workspace\android-support-v7-appcompat\libs\android-support-v4.jar
- <PRE class=plain name="code" snippet_file_name="blog_20131215_1_6735911" code_snippet_id="113243"> Length: 556198
- SHA-1: 4a6be13368bb64c5a0b0460632d228a1a915f58f
- Path: D:\Program Files\adt-bundle-windows-x86_64-20130917\eclipse\workspace\NavigationDrawerDemo(android-support-v7-appcompat)\libs\android-support-v4.jar
- Length: 535319
- SHA-1: 3e1409c5f5c4cdf9fd41db4b22a3eec5b3b10d96
- Jar mismatch! Fix your dependencies</PRE>
- <PRE></PRE>
- <P></P>
- <PRE></PRE>
Found 2 versions of android-support-v4.jar in the dependency list, but not all the versions are identical (check is based on SHA-1 only at this time). All versions of the libraries must be the same at this time. Versions found are: Path: D:\Program Files\adt-bundle-windows-x86_64-20130917\eclipse\workspace\android-support-v7-appcompat\libs\android-support-v4.jar <div class="dp-highlighter bg_plain" sizcache="29" sizset="76"><div class="bar" sizcache="29" sizset="76"><div class="tools" sizcache="29" sizset="76"><strong>[plain]</strong> <a target=_blank title="view plain" class="ViewSource" href="http://blog.csdn.net/tianjf0514/article/details/13278539#">view plain</a><a target=_blank title="copy" class="CopyToClipboard" href="http://blog.csdn.net/tianjf0514/article/details/13278539#">copy</a><a target=_blank title="print" class="PrintSource" href="http://blog.csdn.net/tianjf0514/article/details/13278539#">print</a><a target=_blank title="?" class="About" href="http://blog.csdn.net/tianjf0514/article/details/13278539#">?</a><a target=_blank title="在CODE上查看代码片" style="TEXT-INDENT: 0px" href="https://code.csdn.net/snippets/113243" target="_blank"><img style="POSITION: relative; LEFT: 2px; TOP: 1px" alt="在CODE上查看代码片" src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" /></a><a target=_blank title="派生到我的代码片" style="TEXT-INDENT: 0px" href="https://code.csdn.net/snippets/113243/fork" target="_blank"><img style="POSITION: relative; LEFT: 2px; TOP: 2px" alt="派生到我的代码片" src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" /></a></div></div><ol><li class="alt"><span><span> Length: 556198 </span></span></li><li><span> SHA-1: 4a6be13368bb64c5a0b0460632d228a1a915f58f </span></li><li class="alt"><span>Path: D:\Program Files\adt-bundle-windows-x86_64-20130917\eclipse\workspace\NavigationDrawerDemo(android-support-v7-appcompat)\libs\android-support-v4.jar </span></li><li><span> Length: 535319 </span></li><li class="alt"><span> SHA-1: 3e1409c5f5c4cdf9fd41db4b22a3eec5b3b10d96 </span></li><li><span>Jar mismatch! Fix your dependencies </span></li></ol></div><pre class="plain" style="DISPLAY: none" code_snippet_id="113243" snippet_file_name="blog_20131215_1_6735911" name="code"> Length: 556198 SHA-1: 4a6be13368bb64c5a0b0460632d228a1a915f58f Path: D:\Program Files\adt-bundle-windows-x86_64-20130917\eclipse\workspace\NavigationDrawerDemo(android-support-v7-appcompat)\libs\android-support-v4.jar Length: 535319 SHA-1: 3e1409c5f5c4cdf9fd41db4b22a3eec5b3b10d96 Jar mismatch! Fix your dependencies4、让Activity继承ActionBarActivity,并在activity标签里面添加主题<activity android:theme="@style/Theme.AppCompat.Light" ... >
5、使用到ActionBar的地方通过getSupportActionBar()方法来获取ActionBar。
6、ActionBar的具体使用方法请参照http://blog.csdn.net/tianjf0514/article/details/13246075,再次就不再多作说明,下面具体说明使用library的一些不同的地方。
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
- <item android:id="@+id/action_search"
- android:icon="@drawable/ic_action_search"
- android:title="@string/action_search"
- yourapp:showAsAction="ifRoom" />
- ...
- </menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" yourapp:showAsAction="ifRoom" /> ... </menu>
这是告诉系统,showAsAction使用library的XML attributes。此外还有
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
- <manifest ...>
- <activity uiOptions="splitActionBarWhenNarrow" ... >
- <meta-data android:name="android.support.UI_OPTIONS"
- android:value="splitActionBarWhenNarrow" />
- </activity>
- </manifest>
<manifest ...> <activity uiOptions="splitActionBarWhenNarrow" ... > <meta-data android:name="android.support.UI_OPTIONS" android:value="splitActionBarWhenNarrow" /> </activity> </manifest>
android:uiOptions="splitActionBarWhenNarrow"表示使用系统的Up Navigation。meta-data部分表示使用library的Up Navigation。
menu.xml
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
- <item
- android:id="@+id/action_overflow"
- android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_light"
- android:title="@string/action_websearch"
- android:titleCondensed="@string/action_websearch"
- yourapp:showAsAction="ifRoom|withText"/>
- </menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_overflow" android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_light" android:title="@string/action_websearch" android:titleCondensed="@string/action_websearch" yourapp:showAsAction="ifRoom|withText"/> </menu>
popup_menu.xml
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:icon="@drawable/action_search"
- android:title="@string/action_websearch"/>
- <item
- android:icon="@drawable/action_search"
- android:title="@string/action_websearch"/>
- </menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:icon="@drawable/action_search" android:title="@string/action_websearch"/> <item android:icon="@drawable/action_search" android:title="@string/action_websearch"/> </menu>
MainActivity.java
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.action_overflow:
- if (mPopupMenu == null) {
- mPopupMenu = new PopupMenu(this,
- findViewById(R.id.action_overflow));
- mPopupMenu.inflate(R.menu.popup_menu);
- mPopupMenu.setOnMenuItemClickListener(this);
- }
- mPopupMenu.show();
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_overflow: if (mPopupMenu == null) { mPopupMenu = new PopupMenu(this, findViewById(R.id.action_overflow)); mPopupMenu.inflate(R.menu.popup_menu); mPopupMenu.setOnMenuItemClickListener(this); } mPopupMenu.show(); return true; default: return super.onOptionsItemSelected(item); } }
由于library会识别系统,OS3.0以上使用系统自带的ActionBar,OS3.0以下使用library的ActionBar。所以主题都要准备两套,一套系统用,一套library用。
比如:
- <!-- ActionBar styles -->
- <style name="AppTheme.MyActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
- <item name="android:background">@drawable/common_title_bg</item>
- <item name="android:backgroundSplit">@drawable/common_title_bg</item>
- <item name="android:titleTextStyle">@style/AppTheme.MyActionBar.TitleTextStyle</item>
- <item name="android:actionOverflowButtonStyle">@style/AppTheme.MyActionBar.actionOverflowButtonStyle</item>
- <!-- Support library compatibility -->
- <item name="background">@drawable/common_title_bg</item>
- <item name="backgroundSplit">@drawable/common_title_bg</item>
- <item name="titleTextStyle">@style/AppTheme.MyActionBar.TitleTextStyle</item>
- <item name="actionOverflowButtonStyle">@style/AppTheme.MyActionBar.actionOverflowButtonStyle</item>
- </style>
<!-- ActionBar styles --> <style name="AppTheme.MyActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid"> <item name="android:background">@drawable/common_title_bg</item> <item name="android:backgroundSplit">@drawable/common_title_bg</item> <item name="android:titleTextStyle">@style/AppTheme.MyActionBar.TitleTextStyle</item> <item name="android:actionOverflowButtonStyle">@style/AppTheme.MyActionBar.actionOverflowButtonStyle</item> <!-- Support library compatibility --> <item name="background">@drawable/common_title_bg</item> <item name="backgroundSplit">@drawable/common_title_bg</item> <item name="titleTextStyle">@style/AppTheme.MyActionBar.TitleTextStyle</item> <item name="actionOverflowButtonStyle">@style/AppTheme.MyActionBar.actionOverflowButtonStyle</item> </style>
当在Android3.0以下版本的手机上运行的时候,会出现以下错误,但是此错误不影响程序运行。Style contains key with bad entry: 0x01010300
此错误的原因是因为准备了两套自定义主题,给系统用的那套需要在3.0以上的版本运行,所以3.0以下的版本自然没有这些属性,所以就报错了。
解决办法是把给系统用的那套主题移到values-v14里面去就行了。
检查Project -> Properties -> Java Build Path -> Order & Export -> Android Private Libraries 是否被勾中。
Order and Export有两个属性。调用优先级的问题,越在上边调用优先级越高,例如,在同包同类名的情况下,将优先从上到下执行搜索调用;打勾与不打勾的区别在于导出jar包时是否将这个库的内容也打包到jar中,勾为包含,不勾为不含。
一个使用android-support-v7-appcompat的Navigation Drawer的Demo:http://download.csdn.net/detail/tianjf0514/6462957