Android Learning Note: Android activity bar

Source: Internet
Author: User

After Android3.0 ,Google has made a series of changes to the UI navigation design, and one of the most useful new features is the introduction of ActionBar , used to replace 3.0 the previous title bar, and provides a richer navigation effect.

ActionBar can display the app icon and activity title, as well as the action Item activity item.

main uses of ActionBar:

1. You can set the icon and title of the Activity.

2. Can be used with fragment or drop-down list to achieve the navigation of different pages.

3. You can add an action View to provide quick access to the user.

Enable ActionBar:

(1). above Android3.0 versions are enabled by default ActionBar the. We will find that the new Activity is inherited from Actionbaractivity by default . If you want to turn off ActionBar, we can set the theme to Xxx.noactionbar. Like this:

<activity android:theme= "@android: Style/theme.holo.noactionbar" >

(2). We can also use the code to call the Show () or Hide () method to display or hide the corresponding ActionBar

. before we use actionbar.hide () method, the system defaults to the ActionBar The occupied space is allocated to the interface, and the displayed interface is redrawn. Also call actionbar.show (); The interface will be redrawn as well. If we have a program that needs to show or hide ActionBar frequently , it will not have a good effect. Google provides a property to let ActionBar float on the interface, of course, you can let ActionBar The background is transparent, so it will have a good experience.

(3). Display the Options menu on the active bar

This is to solve the problem that many phones no longer offer the Menu button.

MenuItem provides the setshowasaction (int actionenum) method to display menu items on ActionBar as Action Item .

Actionenum Optional parameter values are as follows:

Show_as_action_always: The MenuItem is always displayed on ActionBar .

Show_as_action_collapse_action_view: Collapses the ACTION VIEW into a regular menu item

Show_as_action_if_room: The MenuItem is displayed if the ActionBar position is sufficient .

Show_as_action_never: Not displayed on ActionBar

Show_as_action_with_text: Displays only the text of the MenuItem on ActionBar .

Of course we can also modify the menu item's XML file to display it on the ActionBar , only need to modify the <item.../> the android:showasaction property.

When the ActionBar space on the top of the phone is not enough,Android has different ways of handling different devices, there is a MENU The button's hand will display a menu item that cannot be displayed on the menu that is fired by the menus button, and the phone without the menu key will be in the ActionBar at the end of the display, a collapsed icon appears, and the remaining menu items appear when you click the icon.

(4). turn the app icon on the activity bar into a clickable icon.

You can call these methods as follows:

Setdisplayhomeasupenabled (Boolean Showhomeasup): Sets whether the app icon can be clicked and adds a left arrow to the icon, typically for fallback to the parent Activity .

setdisplayoptions (int options): controls The display options for ActionBar.

Setdisplayshowhomeenable (Boolean showhome): Sets whether the application icon is displayed.

Sethomebuttonenabled (Boolean enabled): sets whether to turn the application icon into a button that can be clicked.

When you click this icon, the system invokes Activity with android. the onoptionsitemselected () method of the R.id.home ID .

In order to respond to click events, we also need to add the appropriate processing to the onoptionsitemselected method in the Options menu .

(5). Show on the activity bar Action View

There are two ways we can add normal components to the active bar.

1. When you define an action Item , use the android:actionviewclass property to specify the action View implementation class.

2 When you define an action Item , use the android:actionlayout property to specify the action View implementation class.

(6). Use ActionBar Implement Tab Navigation

To display multiple tab tabs on ActionBar, clicking the tab tab navigates to the specified tab Page.

The steps to implement TAB navigation are as follows:

1. set the mode for ActionBar to Tab navigation Setnavigationmode (actionbar.navigation_mode_tabs ), this method can also set the parameter actionbar.navigation_mode_list, which is used to implement the drop-down list.

2. Call ActionBar 's addTab () method to add multiple tab tabs for the active bar , and for each tab tag to add an event listener.

To better illustrate the Tab navigation effect, we often use ActionBar and Fragment together.

Fragment can create multiple pages in one Activity , which is described later.

(7). except Tab In addition to navigating, we can also use ActionBar implement pull-down navigation. The steps are as follows:

1.ActionBar Sets the mode to drop-down list setnavigationmode (actionbar.navigation_mode_list).

2. Call the setlistnavigationcallbacks (Spinneradapter Adapter,actionbar.onnavigationlistener callback) method, Add multiple list items and event listeners for list items to the drop-down list.


Android Learning Note: Android activity bar

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.