Use of actionbar in Android support V7 package (1) Add action item

Source: Internet
Author: User

The following example shows API <11, because there is an actionbar available when API> = 11, so it is out of scope for discussion.

Today, Google released the latest API 18, including a large number of new performances. It was just recently studying actionbarsherlock. Seeing that the latest support V7 package also began to support actionbar, we studied how to use it, I will record a usage method I have learned today as follows. I hope you can introduce it to me.

1. Import the support V7 project. After SDK 18 is updated, you will find thatThe \ Android-SDK \ extras \ Android \ support \ V7 directory has three subfolders:Appcompat \ gridlayout \ mediarouter, which is commonly used in work, so the scope of this article also refers to this project. The import procedure is as follows:

1.1 from existingCodeCreate an android Project


1.2 select the directory where the appcompt project is located and import the project,Be sureCopy projects into Workspace



2. Create a demo project, right-click the project, and select Properties


In the displayed dialog box, select the android tab and click Add.


A dialog box is displayed, asking us to select the project to be referenced. The Project of Support V7 we just imported is displayed. Select it and click OK, our Demo project references the support V7 project.

3. in the manifest file, you can use the actionbar actitity to add the theme attribute. The theme must be @ style/theme. appcompat or its subclass, I am lazy and directly add theme to the application node.

4. Write an XML file under the Res/menu Directory, which will be referenced by the activity to display the action item. It is not much different from the ordinary menu. We recommend that you have icons and title icons for each item. By default, icons are displayed. When there is not enough space on the screen, undisplayed menus are hidden in the list, the value of the title field can only be displayed in the list. Of course, you can also use the logo attribute. When there is a logo attribute, the system will display the icon referenced by the logo attribute instead of the icon referenced by the icon attribute.

<Menu xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: alpha = "http://schemas.android.com/apk/res-auto"> <! -- Be sure to add the custom namespace of the preceding row, otherwise the action item function cannot be implemented through the XML file --> <! -- Note that the suffix of the above custom namespace must be consistent with the prefix of Alpha: showasaction in item, otherwise the effect will not be visible. --> <item Android: id = "@ + ID/action_settings" Alpha: showasaction = "ifroom" Android: icon = "@ drawable/ic_launcher" Android: Title = "AA"/> <item Android: id = "@ + ID/action_settings" Alpha: showasaction = "ifroom" Android: icon = "@ drawable/ic_launcher" Android: Title = "BB"/> </menu>

Note that to implement action item through an XML file, you must customize the namespace, and the extension of the namespace must be consistent with the prefix of showasaction in item, in this example, it is "Alpha"

There are several showasaction attributes:

Ifroom: if there is space, it will be displayed. If there is no space, it will be folded.

Never: Never show

Always: always display

Withtext: displays the icon and text together.

Collapseactionview: collapsed

5. Rewrite the activity that needs to display the actionbar and let it inheritAndroid. Support. v7.app. actionbaractivity. Override oncreateoptionsmenu Method

@ Overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true ;}

In this way, we are done. Processing the click event of action item is the same as processing the normal menu click event. You can override the onoptionsitemselected () method.

Project files: http://download.csdn.net/detail/hkg1pek/5899057

Related Article

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.