How to use Android Actionbar

Source: Internet
Author: User

For this actionbar I think a lot of people want to know exactly how a usage is, and where it all exists and where it can be manipulated and used.
As shown in the following:
<ignore_js_op>
This is the basic content of Actionbar.
The method of obtaining Actionbar is very simple Actionbar = This.getactionbar ();
1.Home icon Area: The icon here can certainly be defined in the manifest file, or it can be defined in the code, but the "qualification" required to request the setting is defined in the code, i.e. the following code needs to be added
Requestwindowfeature (Window.feature_left_icon);
Setfeaturedrawableresource (Window.feature_left_icon, resId);
If you want to do a click-time processing on the home icon, you need to "enable" for this icon:
Actionbar.setdisplayhomeasupenabled (TRUE);
The ID of this home icon is Androi. R.id.home;
The home Click event is captured in the public boolean onmenuitemselected (int featureid, MenuItem item), for example:

[Java]Plain Text view copy code ?
12345678 @Override public boolean onmenuitemselected ( int featureid, MenuItem item { switch (Item.getitemid ()) { case android. R.id.home: toast.maketext ( this 1 ). Show (); break } }


2. The title area, corresponding also has
Actionbar.setdisplayshowtitleenabled (TRUE);
Of course we can also set our own style to the title bar, but that also must be in Setcontentview (R.layout.custom_title);
Requestwindowfeature (Window.feature_custom_title);
You can then enable
GetWindow (). Setfeatureint (Window.feature_custom_title, resId);
Resid is the layout style you want.
3. Tab area: This area is believed to be the most commonly used area, and it also has 3 different style types:
Actionbar.navigation_mode_tabs actionbar.navigation_mode_list Actionbar.navigation_mode_standard
Actionbar.navigation_mode_standard don't have to say much about this area.
And for the previous two kinds as shown: actionbar.navigation_mode_tabs
When the first button is clicked:
<ignore_js_op>
The implementation of the code is actually simple:

[Java]Plain Text view copy code ?
12345678 actionBar.setTitle("选项卡");actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);actionBar.addTab(actionBar.newTab().setText("Tab1").setTabListener(MainActivity.this));actionBar.addTab(actionBar.newTab().setText("Tab2").setTabListener(MainActivity.this));actionBar.addTab(actionBar.newTab().setText("Tab3").setTabListener(MainActivity.this));


You must add the Tablistener event for the tab, and if you want to manipulate the tab event, you only need to implement the listening method.
Implements Actionbar.tablistener
When you click the second button:
<ignore_js_op>
Displays a list of the drop-down that can be clicked, with the following code

[Java]Plain Text view copy code ?
1234567 String[] str = { "1", "2", "3", "4", "5" };actionBar.setTitle("列表");actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, str),MainActivity.this);


The same need to implement the following its click-listen event implements Actionbar.onnavigationlistener implementation of the interface method can be done to the list items you want to do.
4. Menu area: In fact, this area is equivalent to the menu, but it will be put menu to actionbar here only, the event monitoring and layout with the Operation menu is the same.
And if you want to put the menu on the Actionbar, just add a android:showasaction such as the item you need to fill.
He also offers you a few optional types: Ifroom|withtext means that the icon and font never are never, always.
: <ignore_js_op>

When you click Item1 this icon area: A: submenu appears. As the name implies, this pull-down item is from the submenu that belongs to Item1, if you want to achieve this effect, just add a submenu in the menu to achieve this effect, such as the simple implementation of this example:

[Java]Plain Text view copy code ?
010203040506070809101112131415161718192021222324252627 <itemandroid:id="@+id/action_settings"android:icon="@drawable/ic_launcher"android:orderInCategory="3"android:showAsAction="ifRoom|withText"android:title="item1"><menu><itemandroid:id="@+id/save"android:icon="@drawable/ic_launcher"android:orderInCategory="4"android:showAsAction="ifRoom|withText"android:title="item2"/><itemandroid:id="@+id/item8"android:icon="@drawable/ic_launcher"android:orderInCategory="5"android:showAsAction="ifRoom|withText"android:title="item3"/><itemandroid:id="@+id/item9"android:icon="@drawable/ic_launcher"android:orderInCategory="6"android:showAsAction="ifRoom|withText"android:title="item4"/></menu></item>


This will achieve this effect. The listener for item has emphasized that it is the item of the menu, so the handling of the listener event is
public boolean onmenuitemselected (int featureid, MenuItem item)
Knowing this, we believe that the handling of the corresponding click events in these areas is not difficult.
Finally paste the source code, convenient control: <ignore_js_op> myactionbar.rar (982.67 KB, downloaded: 5126)

How to use Android Actionbar

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.