As the new object introduced after Android 3.0, Actionbar can be said to be a convenient and fast navigation artifact. It can serve as the title of the activity, highlighting some key actions of the activity (such as "search", "create", "share", etc.), as a flexible use of the menu, can also achieve similar Tabwidget label function and Drop-down navigation function, The system can be very good according to different screen configuration to adapt to the appearance of Actionbar, with Fragemtn is very powerful.
So, for today's protagonist Actionbar how to add? In the Android3.0 default theme hlolefraphic (holographic) Theme, Actionbar has been created, so as long as the targetsdkversion value is no less than 11, the activity created by default will be Actionbar. For example:
<manifest >
<uses-sdk android:minsdkversion= "4"
android:targetsdkversion= "one"/>
...
Of course, if you don't want to set action Bar for a specific activity, set the activity theme to Theme.Holo.NoActionBar.
or hide the action Bar at run time by calling Hide (). Nature also has show ().
Actionbar Actionbar = Getactionbar ();
Below we from the Drop-down navigation, the window operation, the label navigation three aspects each discussion Actionbar
First, pull down the navigation
The most typical scenario for Drop-down navigation is the use of Google +, with the following effect:
Figure 1;google+
Figure 2: Example of this article
Implementing this effect is divided into the following steps:
1. Initialization of a Spinneradapter
Spinneradapter Mspinneradapter = Arrayadapter.createfromresource (this,
r.array.action_list,
2. Generate a Onnavigationlistener to respond to Actionbar menu item click Action
/**
* Here with fragment, to achieve a different page navigation
/Onnavigationlistener Monnavigationlistener = new Onnavigationlistener ( {
@Override public
boolean onnavigationitemselected (int position, long itemId) {
Fragment newfragment = null;
Switch (position) {case
0:
newfragment = new Fragment1 ();
break;
Case 1:
newfragment = new Fragment2 ();
break;
Case 2:
newfragment = new Fragment3 ();
break;
Default: Break
;
}
Getsupportfragmentmanager (). BeginTransaction ().
Replace (R.id.container, Newfragment, Strings[position])
. commit ();
return true;
}
;
3, will generate a good fit and listener plug to Actionbar
Actionbar = Getactionbar ();
Actionbar.setnavigationmode (actionbar.navigation_mode_list);//navigation mode must be set to Navigation_mode_list
Actionbar.setlistnavigationcallbacks (Mspinneradapter,
Second, the Operation window
First on the effect chart
In the above Operation window, an optional menu item for searching and two custom actionprovider for sharing and setting are added. So how do you add these operations windows to an existing actionbar in an activity? As with creating an optional menu, the menu file that defines Options.xml is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/menu_search" android:actionviewclass= "Android.widget.SearchView" Androi d:icon= "@drawable/ic_menu_search" android:showasaction= "Ifroom" android:title= "search"/> <item Android : id= "@+id/menu_share" android:actionproviderclass= "Android.widget.ShareActionProvider" android:showasaction= "Nev" ER "android:title=" share "/> <item android:id=" @+id/menu_setting "android:actionproviderclass=" Com.exa Mple.tabdemo.MyActionProvider "android:showasaction=" Never "android:title=" Settings "> <menu> <
Item android:id= "@+id/menu_theme" android:actionproviderclass= "Com.example.tabdemo.MyActionProvider" android:showasaction= "Always|withtext" android:title= "replace the theme"/> <item android:id= "@+id/ Menu_system "Android:actionproviderclass= "Com.example.tabdemo.MyActionProvider" android:showasaction= "Always|withtext" Android
: title= "System settings"/> </menu> </item> </menu>
Careful observation shows that each item contains the following two properties:
For the Actionproviderclass property to specify the layout resources used by a build window, you can use either Actionlayout or Actionviewclass, in addition to using Actionproviderclass designation. Searchview and Shareactionprovider are all system-brought actionprovider,myactionprovider that we want to rewrite, and we'll see how to customize a actionprovider later.
The Showasaction property has a total of five values: ifroom, never, always, Withtext, Collapseactionview, and can be mixed.
Ifroom |
Appears in the item, but is hidden in the overflow list if there are 4 or more item (4 or more). Of course A The number is not limited to only 4, depending on the width of the screen |
Never |
Will never be displayed. Only appears in the overflow list, and only the caption is displayed, so when you define the item, it's best Take all the headlines with you. |
Always |
Whether or not it overflows, it always appears. |
Withtext |
The Withtext value indicates action Bar to display the text caption. The Action bar will display this as much as possible. Title, however, if the icon is valid and is limited by the action bar space, the text caption is available Can not show the whole. |
Collapseactionview |
Declares that the operation window should be folded into a button, and the Operation window expands when the user selects the button. Otherwise This action window is visible by default and occupies the active space of the action bar even when it is not applicable. Usually with Ifroom to use together will have effect. |
Note: When your application is running on the Android4.0 (API level 14) or above, there is also an extra mode called "separating the action Bar" that works for the action Bar. When you enable the Split Action Bar mode, a separate horizontal bar appears at the bottom of the screen to show all the action items that the activity runs on a narrow-screen device such as a vertical cell phone. Here we are too much description, interested in their own to study.
Like loading menu, the above XML file is invoked in the Oncreateoptionsmenu method of the activity:
Getmenuinflater (). Inflate (r.menu.options, menu);
Search window, because showasaction= "ifroom", so the search button appears in Figure three
Searchview Searchview = (searchview) Menu.finditem (r.id.menu_ Search)
. Getactionview ();
Sharing window, because showasaction= "never", so only in the overflow menu to see the
shareactionprovider Mshareactionprovider = (shareactionprovider) Menu
. FindItem (R.id.menu_share). Getactionprovider ();
Intent shareintent = new Intent (intent.action_send);
Shareintent.settype ("image/*");
Mshareactionprovider.setshareintent (shareintent);
Settings window, Myactionprovider is our custom Actionprovider
myactionprovider myactionprovider = (myactionprovider) Menu.finditem (
r.id.menu_setting). Getactionprovider ();
Obviously, when the results are successful, as shown in figure three, when the Search button is clicked, the Search button immediately becomes a collapsible operation window as shown in Figure four.
How to customize the Action window, define a class Myactionprovider inherits from Actionprovider, and implements its two-port callback function. As follows:
/** * @ClassName: Myactionprovider * @Description: Customize a window operator, implement constructor and Oncreateactionview can * @author Yuxianglong *
@date 2013-7-11 Afternoon 3:13:44 * * * * * * */public class Myactionprovider extends actionprovider{private context;
Private Layoutinflater Inflater;
Private view view;
Private ImageView button;
Public Myactionprovider {Super (context);
TODO auto-generated constructor stub this.context = context;
Inflater = Layoutinflater.from (context);
View = Inflater.inflate (R.layout.myactionprovider, NULL); @Override public View Oncreateactionview () {//TODO auto-generated the stub button = (Imagevie
W) View.findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
TODO auto-generated Method Stub toast.maketext (context, "It's me, yes", Toast.length_short). Show ();
}
});return view;
}
}
In this way, just refer directly to the options.xml. Run the successful effect as shown in Figure five, click the Overflow menu, set the button out, if you continue to click on it, back up its submenu, because we in the options.xml to the custom Actionprovider assigned submenu.
Of course, the most obvious thing is to deal with the application icon on the action bar, usually the students who play mobile phones should be able to find that many of the application icons are clickable, and most of them are back to the previous activity, or the main activity. So, how do you trigger the application icon, the application icon is also a menu, and its ID is dead, so as long as we are in the Onoptionsitemselected method to catch its Click event, to respond:
@Override Public
Boolean onoptionsitemselected (MenuItem item) {
switch (item.getitemid ())
{
case Android. R.id.home:
Intent Intent = new Intent (this, homeactivity.class);
Intent.addflags (intent.flag_activity_clear_top);
StartActivity (intent);
return true;
Default: Return
super.onoptionsitemselected (item);
}
We have added a flag_activity_clear_top identifier to intent, which is the role of clearing all activities on the stack at homeactivity when returning to Homeactivity. If this is waiting to run the program, if the system version is less than 4.0, it can run normally, to achieve the desired effect, but if the system is greater than or equal to 4.0, then click the application icon is not valid. You must add sethomebuttonenabled=true,4.0 the default is true. If you also want a fallback arrow, add a setdisplayhomeasupenabled (true), and the effect is as follows:
Here I am expanding: Students who have used navigation drawer should understand that clicking on the app icon is usually used as a pull-out navigation drawer. Usually, in that case, the onoptionsitemselected of the activity is transmitted to the onoptionsitemselected of the Actionbardrawertoggle. Not much said, interested students to study themselves, later will be navigation bar written out.
third, navigation option labels
When you want to provide navigation selection labels in an activity, using the Option tab of the Action Bar is a good choice (rather than using the Tabwidget Class) because the system adjusts the Action Bar Options tab to accommodate the needs of different sizes of screens, when the screen is wide enough, The Navigation Options tab is placed in the main action bar, and when the screen is too narrow, the option label is placed in a separate cross bar. As shown below:
To switch between fragmengt using the option label, select an option label to perform a fragment transaction that contains a ViewGroup object for placing the option label associated with each fragment object. The object has a resource ID so that it can be referenced in the switch code of the option label. The activity's layout file activity_main.xml is defined as follows:
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:id=" @+id/container "
android:layout_width=" Match_parent "
Android : layout_height= "match_parent"
tools:context= ". Mainactivity "
The viewgroup here is fragmentlayout. The activity code is as follows:
* * @ClassName: Mainactivity * @Description: Inherits from Fragmentactivity, uses as fragment holder-activity, * realizes Tablistener interface, when the tab is cut to achieve the effect of switching fragment * @author Yuxianglong * @date 2013-7-11 PM 7:40:35 * * */public class Mainactivity extends
Fragmentactivity implements Actionbar.tablistener {private Actionbar actionbar;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Actionbar = Getactionbar (); Actionbar.setnavigationmode (actionbar.navigation_mode_tabs);//navigation mode must be set to Navigation_mode_tabs//For each of the sect
Ions in the app, add a tab to the Action bar.
Actionbar.addtab (Actionbar.newtab (). SetText (R.string.title_section1). Settablistener (this));
Actionbar.addtab (Actionbar.newtab (). SetText (R.string.title_section2). Settablistener (this)); Actionbar.addtab (Actionbar.newtab (). SetText (r.string.title_section3). SettAblistener (this));
@Override public void ontabselected (Actionbar.tab Tab, fragmenttransaction fragmenttransaction) {
When the given tab isselected, show the tabcontents in the////container view.
Fragment fragment3 = null;
Fragment fragment1 = null;
Fragment fragment2 = null;
Switch (tab.getposition ()) {case 0:if (fragment1 = = null) {fragment1 = new Fragment1 ();
} getsupportfragmentmanager (). BeginTransaction (). replace (R.id.container, fragment1). commit ();
Break
Case 1:if (Fragment2 = = null) {Fragment2 = new Fragment2 ();
} getsupportfragmentmanager (). BeginTransaction (). replace (R.id.container, Fragment2). commit ();
Break
Case 2:if (Fragment3 = = null) {Fragment3 = new Fragment3 (); } getsupportfragmentmanager (). BeginTransaction (). replace (R.id.container, Fragment3). commit ();
Break
Default:break; @Override public void ontabunselected (Actionbar.tab Tab, fragmenttransaction fragmenttransaction)
{} @Override public void ontabreselected (Actionbar.tab Tab, fragmenttransaction fragmenttransaction) {
}
}
The effect of running at last is as follows:
We are familiar with some of the common usage scenarios of this actionbar, and we continue to look at Actionbar's appearance styles later. The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.