Add a search and sharing window for the title bar through the actionbar in Android

After Android 3.0, Google has made a series of reforms in UI navigation design. One of the most useful new features is the introduction of actionbar, which is used to replace the title bar before 3.0 and provide richer navigation effects. The main purposes of actionbar are:

1. Provide a dedicated space for identifying the identification of the application and the location of the user.

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

3. Highlight the key operations of activity (such as "search", "create", "share", etc.) and provide users with fast access in a predictable way.

Today, let's briefly sort out how to add "search" and "share" operation windows to the title through the actionbar, as shown in the following figure:

The operation window is a visual component displayed in the operation bar as an alternative to the operation item button. For example, if you have an optional menu item for search, you can use the searchview class instead of the search button on the action bar.

To declare an action window for an item in a menu resource, you can use either the Android: actionlayout attribute or the Android: actionviewclass attribute to specify a layout resource or a visual component class to use, respectively.

Similar to the action window, the action provider (defined by the actionprovider class) replaces an action item with a custom layout, and it also needs to control the behavior of all these items. When you declare an action item for a menu item in the action bar, it not only needs a customized layout to control the appearance of the menu item, but also handles its default events when it is displayed in the overflow menu. Whether in the action bar or in the overflow menu, it can provide a submenu. For example, the extension class shareactionprovider of actionprovider facilitates sharing operations by displaying a valid list of sharing targets in the action bar. With the traditional call action_ The action items of send type intent objects are different. You can declare a shareactionprovider object to handle an action item. This operation provider keeps an action with processing_ Send the drop-down list of the application of the intent object to make this menu item appear in the overflow menu. Therefore, when you use an action provider like this, you don't have to deal with user events about this menu item. To declare an operation provider for an operation item, define the Android: actionproviderclass attribute in the corresponding < item > element in the menu resource, and the provider should use the complete class name.

Step 1: create a menu resource file

Create menu resource files and set item function components for search and sharing.

(1) Create a new menu folder, as shown in the figure

(2) Create a serch in the menu resource folder_ share_ Menu.xml file (custom name)

(3)serch_ share_ Menu.xml component deployment Code:

Step 2: function implementation code in Java mainactivity.java:

Although these functions can be realized with actionbar, the effects are different in various Android versions and customized ROMs, resulting in serious fragmentation. Therefore, Google released a series of material design style controls at the IO conference in 2015. Where toolbar is the control that replaces actionbar. We will also briefly describe the simple usage of toolbar later.

The above is what Xiaobian introduced to you. In Android, you can add a search and sharing window to the title bar through the actionbar. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>