Brief introduction:
Starting with Android 3.0, Android has joined a new Api,actoinbar, and with the release of Android 4.0 and its slow adoption, many apps are starting to use Google's promotional UI design, and Actionbar is a very important part of it. I'm a little bored with iOS design, and I'm interested in the design style of Android 4.0, but for the sake of program compatibility and the problem of the low version, we need to use the Compatibility pack here.
Unfortunately, Google did not release the 3.0 version of the Actionbar Compatibility Pack. Fortunately online has to provide open source for others to develop, Actionbarshelock, hereinafter referred to as ABS.
ABS can make 2.x system can also use Actionbar, and now ABS also provides a lot of other features after 3.0, such as fragement. This is just about the use of Actionbar.
One of the advantages of ABS is that if you use it on more than 3.0 of the machine, it will call the native actionbar of the system. In addition, its use of methods and the system itself is quite similar to the method, if you actionbar itself is already familiar with, then ABS can quickly get started.
Download:
Download the program to the official website: http://actionbarsherlock.com/download.html for Windows students to download the. zip package, as of the time I issued the version of ABS is 4.3.1.
There are "usage" links in the official website, because the software is constantly updated, the tutorial is certainly not strong enough, so the specific place is to crossing the use of the network instructions.
Reference:
1. After downloading the. zip file, extract the Actionbarsherlock folder to a location on your hard disk.
2. Open Eclipse, select: File--new--project--android Project from Existing Code ... Then select the folder you want to import, and finish is fine.
3, right click on the imported project, (should be called Actionbarsherlock), select the most below, properties, in the Open dialog box to find "Android", confirm that the "is library" was checked. So that this project can be used as a library, let our project to reference.
4, open the properties of the personal item, (in this case actionbar), add Library in Android, click Add, and add the library we just imported, there is a green checkmark to add success.
5. After the import is successful, an error usually occurs. The cause of the error is a duplicate reference to the Android-support-v4.jar.
Because this package is quoted in ABS, and we have this package in our project, the two packages are generally not the same, so errors can occur. Just replace the two packs with the same one.
such as: The ABS Libs folder in the Android-support-v4.jar, copied to our project's Libs folder Next, and covered out.
Use:
After the import is successful, we will add Actionbarsherlock to our program.
1. In the Androidmanifest.xml file of the program, modify the theme to read:
[HTML]View Plaincopy
- Android:theme="@style/theme.sherlock.light"
2, open the program file, such as: Mainactivity.java, reference:
[Java]View Plaincopy
- Import Com.actionbarsherlock.app.ActionBar;
- Import Com.actionbarsherlock.app.ActionBar.OnNavigationListener;
- Import com.actionbarsherlock.app.SherlockActivity;
- Import Com.actionbarsherlock.view.Menu;
- Import Com.actionbarsherlock.view.MenuInflater;
- Import Com.actionbarsherlock.view.MenuItem;
Note: The above references are not necessarily all used.
3, if you want to let Actionbar display some menu buttons, then we need to rewrite the activity in the Oncreateoptionsmenu, corresponding to rewrite onoptionsitemselected to respond to the click of these buttons. In addition, the activity must inherit shelockactivity.
[Java]View Plaincopy
- Public class Mainactivity extends Sherlockactivity {
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- }
- @Override
- Public Boolean onoptionsitemselected (MenuItem item) {
- switch (Item.getitemid ()) {
- Case R.id.menu_delete:
- Toast.maketext (This, "Delete", Toast.length_short). Show ();
- Break ;
- }
- return super.onoptionsitemselected (item);
- }
- @Override
- Public Boolean oncreateoptionsmenu (Menu menu) {
- Getsupportmenuinflater (). Inflate (R.menu.activity_main, menu);
- return super.oncreateoptionsmenu (menu);
- }
- }
Where R.menu.activity_main is the layout file for the menu. The contents are as follows:
[HTML]View Plaincopy
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/add"
- android:icon="@android:d rawable/ic_menu_add"
- android:showasaction="Ifroom"
- android:title="Add"
- android:titlecondensed="Add">
- </Item>
- </Menu>
After running, the effect (virtual machine version, API-8 2.2)
This article first to write here, hope and everyone can give this article some guidance.
Reference article: http://blog.csdn.net/icyfox_bupt/article/details/9286387
Use of Actionbarsherlock-(i) configuration