Actionbar for Android

Source: Internet
Author: User

Since android3.0, the action bar replaces optionsmenu. Therefore, the API settings are involved.

1) set <uses-SDK Android: minsdkversion = "4" Android: targetsdkversion = "11"/> to indicate that the application can be deployed on or before android3.0. That is to say, when the device sdkversion = 11, the application starts the action bar.
But how can an application use the action bar API to access the actionbar class. For example, to add a tab and change the action bar style, we need to set minsdkversion to 11.

2) How can you prevent your activity from using actionbar?

I. Settings: <activity Android: theme = "@ Android: style/theme. holo. noactionbar">

The theme in II. I is the built-in theme, that is, the actionbar theme is removed. How can we cancel the actionbar if we customize a topic?
In a custom topic, use Android: windowactionbar and set it to false.

3) it is worth noting that when the actionbar is deleted using the 2) method, calling getactionbar () in the activity will return null.
Otherwise, the actionbar object is returned normally, and the show and hide operations are called.

4) After optionsmenu is defined, I can select a few menuitems that are preferentially displayed in the action bar.

I. In the menu resource: Use the Android: showasaction attribute and set it to ifroom.

II. In the code: menuitem. setshowasaction (show_as_action_if_room)

5) Generally, menuitem has the icon and text attributes, prompting users to recognize menuitem. When both are provided, the action bar displays an icon by default. To display text, you must

I. In the menu resource: Use the Android: showasaction attribute and set it to ifroom | withtext.

Ii. Code: menuitem. setshowasaction (show_as_action_if_room | show_as_action_with_text)

6) if the application is inFragment in progressAdd a menuitem. When you click this menuitem, the activity first processes it before it reaches fragment.

7) by default, the action bar adds an application icon to the leftmost side. When you click this icon, the returned ID is Android. r. id. home. It is also processed in onoptionsitemselected.

I. The general processing is to return to the home page and end the lengthy activity stack.

Ii. Another common processing method is to navigate up to select any activity in the activity stack. The code and effect are as follows. The onoptionsitemselected process is similar to the Code in I:

8) by default, the menuitem displayed on the action bar is displayed in the icon or text of the menuitem.
We can add a view for the menuitem to display the view on the action bar. Note that if the space of the action bar is insufficient to display the view, the menuitem is in the "more" menu, the icon or text is displayed instead of the view.
What is defined is usually used in resources.OrAttribute. For example:


In this case, we usually set the listener for the view to interact with the user. How can I obtain the view corresponding to menuitem in the menu resource?

9) Android provides the tab function for action bar. The effect is as follows:

I. Each tab contains an icon and text. Generally, each tab corresponds to a fragment.

Ii. basic steps to achieve this effect
Getactionbar (). setnavigationmode (navigation_mode_tabs ). Getactionbar () can be called only after setcontextview is called in oncreate of activity ().
Create an actionbar. Tab object: actionbar. newtab (). Here, actionbar is an object rather than a class.
Call tab. settext, seticon, and settablistener. Tablistener is associated with fragment. In this way, the association between tab and fragment is realized.
Add a tab to the last actionbar. addtab.

Iii. tablistener structure:

Iv. getselectednavigationindex () can be used to obtain the Selected tab index. When a user switches the activity in the front and back of the page, the interface is restored Based on the selected tab.

10) navigation is required for a website or game. In addition to tab navigation, the android system provides the drop-down navigation function for action bar.

I. getactionbar (). setnavigationmode (actionbar. navigation_mode_list); enables the drop-down navigation function.

Ii. actionbar. setlistnavigationcallbacks (mspinneradapter, mnavigationcallback); set the navigation items in the drop-down navigation and event handling after clicking the navigation items.
Mspinneradapter is an instance of the spinneradapter class, that is, all navigation items here.
Mnavigationcallback is an actionbar. onnavigationlistener class instance, that is, the event listening processor.

Iii. Create a spinneradapter
Spinneradapter mspinneradapter = arrayadapter. createfromresource (this, R. array. action_list, Android. R. layout. simple_spinner_dropdown_item );
Parameter 1: Context
Parameter 2: String Array resource <string-array name = "action_list">
Parameter 3: Layout of each navigation item in the drop-down navigation.

Iv. Create actionbar. onnavigationlistener
This event listener is mainly used to process the event when the navigation item is clicked: onnavigationitemselected.
Public Boolean onnavigationitemselected (INT position, long Itemid ).

11) the action bar is also part of the UI. How can we make it consistent with the style of the UI of the activity?

The I. actionbar. setbackgrounddrawable member sets the background image of the action bar. We recommend that you do not use a fixed pixel image.

II. The actionbar. setdisplayuselogoenabled member sets the icon displayed on the pneumatic bar. Instead of using the application icon, you can use the Android: logo in <Application>.

III. The topic is applied for the action bar. By default, the action bar uses the theme. holo topic. It is opposite to theme. holo. Light. The latter is white and black.
The setting method is as follows: add a topic for the activity in manifest. xml. Of course, this topic not only includes the action bar style.

Iv. Style of action bar:
Android: actionbartabstyle: tab Style
Android: actionbartabbarstyle: bar style under Tab
Android: actionbartabtextstyle: Text style in Tab
Android: actiondropdownstyle: drop-down navigation and floating menu style
Android: actionbuttonstyle: background image of the button

V. How do I customize a topic? The usual practice is to inherit, for example, inherit theme. holo:

Vi. It is easy to apply this custom topic:
<Application Android: theme = "@ style/customactionbar".../>

VII. Of course, if your activity does not use the action bar, there will be no more.
Set the Android: windowactionbar of the activity to false.

VIII. Expansion: Set the Android: windownotitle of the activity to enable the title bar in the question bar.

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////

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.