"Turn" Pro Android Learning Note (49): ActionBar (2): Action icon Area

Source: Internet
Author: User

Directory (?) [-]

    1. The hidden and reality of Actionbar
    2. Actionbar's action icon area
The hidden and reality of Actionbar

ActionBar bar = Getactionbar ();
Bar.  hide (); Hide Action Bar
Bar. Show (); Show Action Bar

Actionbar's action icon area

The right side of the Actionbar is the Action icon area, as shown in

This is the menu area of the Actionbar, which, as part of the Options menu, selects a portion of the item to display in Action Bar. Let's look at the source code first, nothing special. The small example is to add in the previous learning example, directly using the inheritance.

public class ActionAreaTestCase2 extends homepresstestcase1{
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (R.menu.bar, menu);
return true;
}

@Override
public boolean onoptionsitemselected (MenuItem item) {
Showinfo ("Select:" + item.gettitle ());
return super.onoptionsitemselected (item);
}
}

The key to see how R.menu.bar XML is set up.

<?xml version= "1.0" encoding= "Utf-8"?>
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@+id/menu_action_icon1"
Android:title= "Action1"
android:icon= "@drawable/creep001"
android:showasaction= "Ifroom"/><!--compared to the general item, the Showasaction is added to show that in the action bar, in addition to Ifroom, there is always, never, Withtext. You can also use menuitem.setshowasaction (int) in your code to achieve
<item android:id= "@+id/menu_action_icon2"
Android:title= "Action2"
android:icon= "@drawable/creep002"
android:showasaction= "Ifroom"/>
<item android:id= "@+id/menu_action_icon3"
Android:title= "Action3"
android:icon= "@drawable/creep003"
android:showasaction= "Ifroom"/>
... ...
</menu>

If we ask for too many menu items to be displayed in action Bar, then the normal Optionsmenu position is displayed, for example, we set 6 menu items, and the properties are: android:showasaction= "Ifroom", Shown below:

If some properties are not set to showasaction in the menu's XML file, the item is displayed as a regular menu. For example: Normal item is a normal menu item:

If we do not have the icon set, only the title is displayed, the text is shown, and the XML fragment is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@+id/menu_action_icon1"
Android:title= "Action1"
android:showasaction= "Ifroom"/> <!--No example of icon--
<item android:id= "@+id/menu_action_icon2"
Android:title= "Action2"
android:icon= "@drawable/creep002"
android:showasaction= "Ifroom|withtext"/> <!--show an example of icon and text at the same time-
... ...
</menu>

The example code covered in this blog post can be downloaded in Pro Android Learning: Actionbar Small example.

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (49): ActionBar (2): Action icon Area

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.