Use of Actionbarsherlock-(i) configuration

Source: Internet
Author: User

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
    1. Android:theme="@style/theme.sherlock.light"

2, open the program file, such as: Mainactivity.java, reference:

[Java]View Plaincopy
    1. Import Com.actionbarsherlock.app.ActionBar;
    2. Import Com.actionbarsherlock.app.ActionBar.OnNavigationListener;
    3. Import com.actionbarsherlock.app.SherlockActivity;
    4. Import Com.actionbarsherlock.view.Menu;
    5. Import Com.actionbarsherlock.view.MenuInflater;
    6. 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
  1. Public class Mainactivity extends Sherlockactivity {
  2. @Override
  3. public void OnCreate (Bundle savedinstancestate) {
  4. super.oncreate (savedinstancestate);
  5. Setcontentview (R.layout.activity_main);
  6. }
  7. @Override
  8. Public Boolean onoptionsitemselected (MenuItem item) {
  9. switch (Item.getitemid ()) {
  10. Case R.id.menu_delete:
  11. Toast.maketext (This, "Delete", Toast.length_short). Show ();
  12. Break ;
  13. }
  14. return super.onoptionsitemselected (item);
  15. }
  16. @Override
  17. Public Boolean oncreateoptionsmenu (Menu menu) {
  18. Getsupportmenuinflater (). Inflate (R.menu.activity_main, menu);
  19. return super.oncreateoptionsmenu (menu);
  20. }
  21. }


Where R.menu.activity_main is the layout file for the menu. The contents are as follows:

[HTML]View Plaincopy
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  2. <item
  3. android:id="@+id/add"
  4. android:icon="@android:d rawable/ic_menu_add"
  5. android:showasaction="Ifroom"
  6. android:title="Add"
  7. android:titlecondensed="Add">
  8. </Item>
  9. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.