Android's official navigation bar Actionbar

Source: Internet
Author: User

I. Overview of Actionbar

Actionbar is a new component after android3.0, mainly used to mark the application and the location of the user and provide relevant operations and global navigation capabilities. Let's take a look at how to use Actionbar, before we actually use it, we should first understand the structure of Actionbar.

According to the official documentation, we see that the entire actionbar can be divided into 4 sections, as follows:

1, app icon: Mainly used to display the logo of the app, if the current interface is not a first-level interface, you can also show back navigation.

2. View Control: Used to toggle different views or display non-interactive information such as app title.

3, Action Buttons: used to display the most important action button in the app, if too many actionbar will be transferred to the action overflow, long press will show the operation name. According to the documentation, the total width of the Action buttons does not exceed 50% of Actionbar.

4, Action overflow: used to store the display of relatively less use of the Operation button.

After understanding the basic structure of Actionbar, let's take a look at how to use Actionbar.

Second, the use of Actionbar

Because Actionbar is new after android3.0, if you use Actionbar under the API level 11, you need to import the V7 support package, where we need to add a support package for a compatible version, You can then use the Actionbar in the following two steps.

1. Create activity is inherit actionbaractivity

2. Set the theme of the app or activity to @style/theme.appcompat.light

You can see the following as you run the program:

  If you do not want to show actionbar, we can use the following code:

         Get ActionBar object        ActionBar ActionBar =getsupportactionbar ();        Call the Hide method to hide ActionBar        actionbar.hide ();        Call the Show method to show ActionBar        //actionbar.show ();

According to the structure of Actionbar, we first understand the operation of the app icon area, this part mainly has two parts, part is used to show the logo, part is used to show the return key navigation, the main operation of the return key navigation is as follows:

1. Return key navigation display and hide: setdisplayhomeasupenabled (Boolean flag)

2. Return key navigation icon settings: Sethomeasupindicator

3, the Return key event processing: Rewrite the onoptionsitemselected method, make the following judgment

if (id==android. R.id.home) {            Toast.maketext (this, "clicked back!!) ", Toast.length_short). Show ();            return true;}

As follows:

Main operation of logo area:

  1, the logo display and hide:

Setdisplayshowhomeenabled (Boolean flag) setdisplayuselogoenabled (Boolean flag)

  2, the logo settings: Setlogo

Set as follows:

The second part is the View control area, this area can be placed more content, in addition to for view navigation Spinner, tabs, Shareactionprovider, can also display the title and other information, custom view and so on. This content we first look at the title section, the rest of the next time to do a detailed introduction.

  Main operation:

1. Display and hide title: setdisplayshowtitleenabled (Boolean flag)

2. Set the main title: Settitle

3. Set Subtitle: Setsubtitle

4. Set the custom View:setcustomview

Display effects such as:

Actions buttons and action flow are similar in that they are used to hold the display action button, so how do you show the action button in Actionbar?

Here, we can be divided into two steps:

1, rewrite Oncreateoptionsmenu, this method is used to generate the menu button, there are two ways, one way to write an XML file, and the other is Java code generation, here we choose to write XML.

2. Rewrite onoptionsitemselected, which is used to respond to menu actions.

The XML content is as follows:

<menu xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:app= "http://schemas.android.com/ Apk/res-auto "    xmlns:tools=" Http://schemas.android.com/tools "tools:context=". Mainactivity ">    <item android:id=" @+id/action_search "android:title=" search "        android:orderincategory=" "App:showasaction=" Always "        android:icon=" @drawable/ic_launcher "/>    <item android:id=" @+id/ Action_share "android:title=" Share "        android:orderincategory=" app:showasaction= "Never"/> <item    Android:id= "@+id/action_collection" android:title= "Favorites"        android:orderincategory= "app:showasaction=" Never "/>    <item android:id=" @+id/action_font "android:title=" Font size "        android:orderincategory=" app: Showasaction= "Never"/></menu>

Showasaction is a very important property, its value determines the menu display area, respectively has never, always, ifroom and so on

The effect is as follows:

  To learn more about the small partners, you can click to download the source code , run the test yourself.

Jerry Education
Source:http://blog.csdn.net/jerehedu
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Android's official navigation bar 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.