Android ActionBar usage

Source: Internet
Author: User

1. What is ActionBar?

If you have a picture and the truth, you will know about it.

For most applications, the Operation bar can be divided into four different functional areas.


1. Application icons

The application icon is the icon of your application. Place your own logo in the application icon position. Note: If the application does not have the top-level interface at the time, place a left arrow on the left of the icon to indicate the "up" button, so that you can return to the top-level interface. For more details about the "up" button, see the Navigation-Navigation section.

 

The application icon and the status of the "up" button.

2. View Control

If your application displays data in multiple views, this area allows you to switch between views. You can use the drop-down menu or label control.

If your application does not have multiple views, you can display inoperable content, such as the title or brand information.

3. Operation button

Displays the most important actions in your application. If the icon cannot be placed, it is automatically moved to the "other operations" menu.

4. Other operations

Place less operations in this menu.

 
 

 


The above is a google + application. It can be understood that ActionBar is a class for convenient navigation and operations. Apple is the style of the bottom navigation, and Android is the style of the top navigation. The main purpose of the operation bar is:


Highlight important operations (such as "new" and "Search") and make it easy to use.
Provides a unified navigation and view switching experience within the application.
Rarely used functions are collected to other operation menus to reduce the messy layout on the interface.
It provides a space for your application to display its features.
Ii. How to Use ActionBar

Starting from Android3.0 (API level 11), the Action Bar includes Theme used in all activities. holo topic (or a subclass that inherits the Activity). If you do not want to set the Action Bar for a specific Activity, set the Activity topic to Theme. holo. noActionBar. If you want programs to use ActionBar in versions earlier than 3.0, you can use the open-source component: ActionBar Sherlock.

1. Add an operation item. What is an operation item? See the figure below: it is the place specified by the Red Line. The corresponding layout file is xml in the menu folder. Use the following in activity:


 

@Override public boolean onCreateOptionsMenu(Menu menu) {     // Inflate the menu; this adds items to the action bar if it is present.      getMenuInflater().inflate(R.menu.main, menu);     return true; }  @Override public boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.main, menu);  return true; }  

Is it very familiar? The above summary: Actionbar is to replace the tittle bar and menu before 3.0. Then, how do I call the onOptionsItemSelected () method for the corresponding menu click event,
 

@ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case R. id. menuitem1: Toast. makeText (this, "Menu Item 1 selected", Toast. LENGTH_SHORT ). show (); break; case R. id. menuitem2: Toast. makeText (this, "Menu item 2 selected", Toast. LENGTH_SHORT ). show (); break; default: break;} return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {switch (Item. getItemId () {case R. id. menuitem1: Toast. makeText (this, "Menu Item 1 selected", Toast. LENGTH_SHORT ). show (); break; case R. id. menuitem2: Toast. makeText (this, "Menu item 2 selected", Toast. LENGTH_SHORT ). show (); break; default: break;} return true;} Then you can operate actions. Let's take a look at the menuxml file, [java] view plaincopyprint? <Menu xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: id = "@ + id/action_settings" android: orderInCategory = "100" android: showAsAction = "never" android: title = "@ string/action_settings"/> <item android: id = "@ + id/nba" android: orderInCategory = "100" android: showAsAction = "ifRoom" android: title = "@ string/nba"/> <item android: id = "@ + id/footbal" android: orderInCategory = "100" android: showAsAction = "never" android: icon = "@ drawable/ic_launcher" android: title = "@ string/footbal"/> </menu> <menu xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: id = "@ + id/action_settings" android: orderInCategory = "100" android: showAsAction = "never" android: title = "@ string/action_settings"/> <item android: id = "@ + id/nba" android: orderInCategory = "100" android: showAsAction = "ifRoom" android: title = "@ string/nba"/> <item android: id = "@ + id/footbal" android: orderInCategory = "100" android: showAsAction = "never" android: icon = "@ drawable/ic_launcher" android: title = "@ string/footbal"/>

</Menu> it should be noted that the android: showAsAction attribute has several numeric values, each of which represents a different meaning. You can take a look at it when using it. In addition, the display position of actions is associated with the screen size.


2. Hide Navigation. Navigation is this thing. on mobile phones, some exist in the form of a physical key, which is set by the device itself. Use the following code to cancel the Navigation:


 

getWindow().   getDecorView().   setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);  getWindow().  getDecorView().  setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);  

3. One of the most practical functions of ActionBar is to return to the previous interface.

 

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.