Android user interface-operation bar (action bar 1)

Source: Internet
Author: User

The operation column is a window that identifies the application and user location, and provides users with operation and navigation modes. In most activities that need to highlight user operations or global navigation, you should use the operation column, because the operation column can provide users with consistent interfaces across applications, in addition, the system can adapt to the appearance of the Operation bar according to different screen configurations. You can use the actionbar object APIs to control the behavior and visibility of the Operation bar. These APIs are added to android3.0 (API level 11.

The main purpose of the operation bar is:

1. A dedicated space provided to identify the trademark of the application and the location of the user.

The left side of the space is the application icon or logo, and the Activity title. However, if it is a navigation tag that identifies the current view object like the selected tag, you can choose to delete the title of the activity.

2. provide consistent navigation and visual experience between different applications.

The operation Bar provides built-in navigation labels for switching between fragment. It also provides a drop-down list to replace the navigation mode or optimize the current visual effect (such as a list sorted by different conditions.

3. highlight key activities (such as "Search", "CREATE", and "share"), and provide quick access to users in foreseeable ways.

For key user operations, you can place option menu items as operation items directly in the Operation column to provide quick access. The operation item also provides an operation window that provides an embedded window for more direct operations. Menu items that are not improved to operation items are valid in the overflow menu. You can use the menu buttons on the device (when there is a button on the device ), you can also use the overflow menu button in the Operation bar (when the device does not contain the menu button) to display these operation items.

 

Figure 1. The operation column in the honeycomb gallery application, starting from the left, is the logo, navigation option label, and Operation item (an overflow menu button inserted on the right ).

Note: If you are looking for information about the context operation bar, see the "menu" guide.

Add operation bar

From android3.0 (API level 11), the Operation bar is included in all theme usage. when the targetsdkversion or minsdkversion attribute is set to "11" or a greater value in the activity of the hole topic (or its subclass), this topic is the default Topic 1. For example:

<Manifest...>
<Uses-SDK Android: minsdkversion = "4"
Android: targetsdkversion = "11"/>
...
</Manifest>

In this example, the minimum API version required by the application is 4 (Android 1.6), but it also requires that the target API version be 11 (Android 3.0 ). In this way, when the application runs on android3.0 or a later version, the system will provide a panoramic topic for each activity. In this way, each activity will contain an operation bar.

If you want to use the actionbar API to add the navigation mode and modify the style of the Operation bar, you should set the minsdkversion attribute to "11" or a larger value. If you want your application to support the old Android version, you can still run the old Android version on devices that support API level 11 or later, use the restricted droid. If you run the restricted droid, what is the default topic? 1 ,. It is a valid actionbar API set in the overflow menu.

Delete operation bar

If you do not want to operate a special activity, set the activity topic to theme. holo. noactionbar, for example:

<Activity Android: theme = "@ Android: style/theme. holo. noactionbar">

You can also hide the operation bar at runtime by calling the hide () method, for example:

Actionbar =
Getactionbar ();
Actionbar. Hide ();

When the operation bar is hidden, the system will adjust your activity to fill in the currently valid screen space. You can use the show () method to display the operation column again.

When hiding and deleting the operation column, be careful when you try to adapt to the space occupied by the Operation column, resulting in the re-layout of the activity. If your activity regularly hides and displays the operation bar, you may want to use the overwrite mode. Overwrite mode draws the operation bar at the top of the activity, rather than in the area of the screen they own. In this way, your layout remains unchanged when the operation bar is hidden and re-displayed. To use the overwrite mode, you must create a topic for the activity and set the Android: windowactionbaroverlay attribute to true.

Tip: If you have a custom activity topic that deletes the operation bar, it sets the Android: windowactionbar style attribute to false. However, if you use a topic in the delete operation column, the Operation Column cannot be displayed in the create window. Therefore, you cannot add an operation bar to this activity later. Because the getactionbar () method returns NULL.

Add operation item

In some cases, you may want users to directly access a project in the option menu. Therefore, you should declare the menu items displayed in the Operation bar as an operation item. An operation item can contain an icon or text title. If a menu item is not displayed as an operation item, the system places it in the overflow menu. The overflow menu can be displayed either through the menu button of the device or an additional button in the Operation column.

When the activity is started for the first time, the system will call the oncreateoptionsmenu () method to assemble the operation bar and overflow menu for your activity. In this callback method, the menu item resources defined in the XML file should be loaded, such:

@ Override
Public Boolean oncreateoptionsmenu (menu ){
Menuinflater Inflater = getmenuinflater ();

Inflater. Inflate (R. Menu. main_activity, menu );
Return true;

}

Figure 2. Two operation items with icons and text titles, and an overflow menu button.

In the XML file, you can request to display a menu item as an operation item by declaring the Android: showasaction = "ifroom" s attribute to the <item> element. In this way, the menu item can be displayed in the Operation column only when there is a valid space. If there is not enough space, this menu item is displayed in the overflow menu.

If your menu items support titles and icons-with Android: Title and Android: icon properties-by default, only icons are displayed for the operation items. To display the text title, add withtext settings to the Android: showasaction attribute, for example:

<? XML version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: Id = "@ + ID/menu_save"
Android: icon = "@ drawable/ic_menu_save"
Android: Title = "@ string/menu_save"
Android: showasaction = "ifroom | withtext"/>
</Menu>

Note: The withtext value indicates that the text title should be displayed in the Operation column. The title is displayed as much as possible in the Operation column. However, if the icon is valid and restricted by the space in the Operation column, the text title may not be displayed completely.

When you select an operation item, the activity receives a callback from onoptionsitemselected (). You must pass the ID supported by the Android: Id attribute to this method.

Defining the Android: Title attribute for each menu item is crucial, even if you do not declare a title for the Operation item. The reason is as follows:

1. If there is not enough space in the Operation bar to display the operation items, the menu items are displayed in the overflow menu and only the title is displayed;

2. The screen reader should read the menu item title for the visually impaired user;

3. If you only use icons to display an operation item, you can press the item long and use the operation item title to display the prompt information.

Note: If you add a menu item from the fragment object, use the oncreateoptionsmenu callback method of the fragment class. When you select one of the fragment menu items, the system uses the onoptionsitemselected () method corresponding to the fragment object. However, the activity has the opportunity to handle this event first, because the system will call the same callback method of the activity before calling the onoptionsitemselected () method of the corresponding fragment object.

You can also declare a menu item so that it is always displayed as an operation item, instead of being placed in the overflow menu when the space is insufficient. In most cases, you should not use the always attribute value to force a menu item to always be displayed in the Operation bar. However, when an operation window is provided that does not provide default operations for the overflow menu, you must always display a menu item. However, be cautious that too many operations will create a messy UI and cause layout problems on narrow screen devices. The best way is to use the ifroom property value, which allows the system to move the menu item to the overflow menu when the space in the Operation bar is insufficient.

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.