First, Action View
The action view is a substitute for the action Button on the Actionbar. To declare a view, you need to define the layout resources and layout classes for the view separately using either of the two properties of Actionlayout and actionviewclass .
Here's how to define the Searview component:
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:yourapp= "Http://schemas.android.com/apk/res-auto" > <item android:id= "@+id/action_search" Android: title= "@string/action_search" android:icon= "@drawable/ic_action_search" yourapp:showasaction= "Ifroom|col Lapseactionview "yourapp:actionviewclass=" Android.support.v7.widget.SearchView "/></menu>
The Note:showasaction property also contains Collapseactionview, which is an optional value that declares that the view is shrunk to a button.
If you want to configure views (such as adding event listeners), you can do these things in the Oncreateoptionsmenu () function. If you want to get a view object you can use the static method Menuitemcompat.getactionview ()
@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main_activity_actions, menu); MenuItem Searchitem = Menu.finditem (R.id.action_search); Searchview Searchview = (searchview) menuitemcompat.getactionview (Searchitem); Configure the search info and add any event listeners ... return super.oncreateoptionsmenu (menu);}
In API 11 or higher
Gets the view object using thegetActionView()函数
Menu.finditem (R.id.action_search). Getactionview ()
For more information about the search widget see Creating a search Interface.
Handling Collapsible Action views
To protect the space of the action bar, you need to shrink your view to a button, and you need to add a Collapseactionview value to the Showasaction property for the view as described above.
When the user selects the view, the system expands the view, and you do not need to respond to this item in the onoptionsitemselected () function. If you want to update your activity based on your visible view, you can accept the callback function when the view shrinks and stretches.
@Overridepublic boolean oncreateoptionsmenu (Menu menu) { Getmenuinflater (). Inflate (R.menu.options, menu); menuitem menuitem = menu.finditem (R.id.actionitem); ... // when Using the support library, the setonactionexpandlistener () method is // static and accepts the menuitem object as an argument menuitemcompat.setonactionexpandlistener (menuItem, new Onactionexpandlistener () { @Override public boolean onmenuitemactioncollapse (MenuItem item) { // do something when collapsed &Nbsp; return true; // return true to collapse action view } @Override public boolean Onmenuitemactionexpand (Menuitem item) { // Do something when expanded return true; // return true to expand action view } });}
Second, Action Provider
Like Action view, action provider replaces the action button with a custom layout. However, unlike action view, action provider controls the behavior of all actions, and when the action provider is pressed, the submenu can pop up.
For what action provider, you need the <item> tag in the menu to provide the Actionviewclass property with the class name of a Actionprovider fully qualified class.
You can create your own acion provider by inheriting actionprovider the class. Because Actionprovider defines its own behavior, so there is no need to onoptionsitemselected () method, if necessary you can still set the listener in onoptionsitemselected () in case you need to perform other action at the same time, but still return false so that action provider can also accept callback function executes its intent action.
Shareactionprovider
Use Shareactionprovider to add a "share" action.
<?XML version="1.0"encoding="Utf-8"?>
<menu xmlns:android="Http://schemas.android.com/apk/res/android"
Xmlns:yourapp="Http://schemas.android.com/apk/res-auto" >
<item Android:id="@+id/action_share"
Android:title="@string/share"
yourapp:showasaction="Ifroom"
yourapp:actionproviderclass="Android.support.v7.widget.ShareActionProvider"
/>
...
</menu>
now Action Provider can control its options and handle its display and action, but you have to provide a title for the option. The only thing left to do is to define a intent for sharing. To do this, you will edit the oncreateoptionsmenu
Menuitemcompat.getactionprovider () and when pressed
menuitem when processing. Then use the returned Shareactionprovider setshareintent the () method and passes it a action_send intent with the appropriate additional content.
You want to call Setshareintent () when Oncreateoptionmuenu () initializes the share action, because the user's environment may change, You must update your intentions at any time, and the content that you can share by calling Setshareintent () again will change.
private shareactionprovider mshareactionprovider; @Overridepublic boolean Oncreateoptionsmenu (Menu menu) { getmenuinflater (). Inflate (R.menu.main_ Activity_actions, menu); // set up shareactionprovider ' s Default share intent menuitem shareitem = menu.finditem ( R.id.action_share); mshareactionprovider = (ShareActionProvider) menuitemcompat.getactionprovider (ShareItem); mshareactionprovider.setshareintent (Getdefaultintent ()); return Super.oncreateoptionsmenu (menu);} /** defines a default (dummy) share intent to initialize the action provider. * However, as soon as the actual content to be used in the intent * is known or changes, you must update the share intent by again calling * Mshareactionprovider.setshareintent () */private intent getdefaultintent () { intent intent = new intent (intent.action_send); Intent.settype ("image/*"); return intent;}
Custom Action provider
Creating your own action provider allows you to re-use and manage the dynamic action Item,android has already provided an implementation way, by inheriting the Actionprovider class and implementing its callback function, it is important to have the following two:
ActionProvider()
This constructor is passed to your application context and you should save this in a member variable that you can later use in other callback functions.
onCreateActionView(MenuItem)
Define the view here, use Context
get instantiation LayoutInflater
from the constructor and inflate from XML resource ? ? You view the layout and then bind the event listener.
Third, Navigation Tabs
Once you have identified where the fragment appears in the layout file, here are the basic steps for tagging:
Implement the ActionBar.TabListener
interface. This interface provides callbacks for tab events, such as when the user presses one so can swap the tabs.
for all tab want to add, instantiate anActionBar.Tab
and set theActionBar.TabListener
by callingsetTabListener()
. Also set the tab ' s title and withsetText()
(and optionally, an icon withsetIcon()
).
Then add all tab to the action Bar by calling addTab()
.
/** constructor used each time a new tab is created. * @param activity the host activity, used to instantiate the fragment * @param tag the identifier tag for the fragment * @param clz the fragment ' S class, used to instantiate the fragment *//* The following are each of the callbacks */// check if the fragment is already initialized// if not, instantiate and add it to the activity// if it exists, simply attach it in order to show it// Detach The fragment, because another one is being attached// user selected the Already selected tab. usually do nothing.
Notice that Setcontentview () isn't used, because we use the root//Android. R.id.content as the container for each fragment//Setup action Bar for tabs
Iv. Drop-down Navigation
Here are the basic steps to add drop-down navigation
Create a SpinnerAdapter
that provides, the list of selectable items for the Drop-down and the layout of the if drawing each item in the list .
Implement ActionBar.OnNavigationListener
To define the behavior this occurs when the user selects a item from the list.
During your activity ' s onCreate()
method, enable the action bar ' s Drop-down list by calling setNavigationMode(NAVIGATION_MODE_LIST)
.
Set The callback for the Drop-down list setListNavigationCallbacks()
with . For example:
Actionbar.setlistnavigationcallbacks (Mspinneradapter, mnavigationcallback);
This method takes your SpinnerAdapter
and ActionBar.OnNavigationListener
.
ActionBar navigation menu, content availability, and drop-down menu