A description
Android 3.0 appears after 3.0 called title bar display position on the title bar
You can display the app's icon and activity's title
Created in the same way as the system menu, the difference is: android:showasaction= ""
Ifroom display when there is room in the Actionbar
Always shows up in Actionbar
Never is never shown in Actionbar.
Withtext display text
Collapseactionview can be folded
Second, the functions provided in Actionbar
1, menu item showing the options menus
2, can make the application's icon to return the function of the upper level interface
3, provides interactive view and action view
4, you can implement tab-based navigation, you can switch fragment
5, drop-down navigation
6, the use of Actionprovider
Third, how to create
1, create each menu item declaration in the Res/menu file <item/>
Display mode: showasaction
Icons: Icon
2, in activity, overrides the parent class's Oncreateoptionsmenu (Menu menu), loading the menus resource
3, in activity, overrides the Onoptionsitemselected (MenuItem item) of the parent class to handle the click event of each item
Four, split the action bar API 14 or more
Add properties in the manifest file <application/> or <activity/>: android:uioptions= "Splitactionbarwhennarrow"
Five. Start the navigation icon
Function: You can make the icon of the current app as a clickable icon
object to get Actionbar: Getactionbar ()
Setdisplayshowhomeenabled setting whether to display an icon for an application
The icon for the Setdisplayhomeasupenabled settings application can be clicked, and a left arrow appears to the left of the icon
Sethomebuttonenabled Settings app icon can be clicked, but no arrows
Get the application navigation icon id:android. R.id.home
Six, the common method in Actionbar
Actionbar.isshowing () to determine whether the current ActionBar is being displayed
Actionbar.show () Show ActionBar
Actionbar.hide () Hide ActionBar
Seven. Use of Action View
You can edit action items, such as the Searchview control, which can be displayed directly in the Actionbar
There are two ways of implementing this:
1, the Actionviewclass attribute implements the Collapsibleactionview class
2, the Actionlayout property displays the normal layout page
Eight, ActionBar tab navigation function through the Option tab to switch fragment
ActionBar + Fragment
1, gets the Actionbar object, and sets the navigation mode to tabs
Setnavigationmode (int mode) Set Navigation mode
Navigation_mode_standard default mode
Navigation_mode_list list mode
Navigation_mode_tabstab Tag Mode
2, let the current class implement Interface Tablistener, rewrite 3 methods
3, create each tab item and add it to Actionbar
Actionbar.tab Tab = Actionbar.newtab ();
Tab.seticon ();//Set icon
Tab.sttext ();//Set the text to be displayed
Tab.settablistener ();//Set monitoring
Actionbar.add (tab);//Add tab to ActionBar
IX: Removal of Actionbbar
Before the Setcontentview requestwindowfeature (window.feature_no_title);
Or: Android:theme= "@android: Style/theme.black.notitlebar"
To implement a fragment switch instance:
[Java]View PlainCopyprint?
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- ActionBar = Getactionbar ();
- //Get navigation mode
- Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs);
- //Create Actionbar.tab item and add it to Actionbar
- Actionbar.tab Tab = Actionbar.newtab ();
- Tab.settext ("headline");
- Tab.seticon (R.drawable.ic_launcher);
- Tab.settablistener (this);
- Actionbar.addtab (tab);
- tab = Actionbar.newtab ();
- Tab.settext ("Sport");
- Tab.seticon (R.drawable.ic_launcher);
- Tab.settablistener (this);
- Actionbar.addtab (tab);
- Actionbar.addtab (Actionbar.newtab (). SetText ("Finance")
- . SetIcon (R.drawable.ic_launcher). Settablistener (this));
- Actionbar.addtab (Actionbar.newtab (). SetText ("car")
- . SetIcon (R.drawable.ic_launcher). Settablistener (this));
- }
- @Override
- public void ontabselected (Tab tab, fragmenttransaction ft) {
- //TODO auto-generated method stub
- Fragmentnew f = new Fragmentnew ();
- Bundle B = new bundle ();
- int position = Tab.getposition ();
- B.putint ("position", position);
- F.setarguments (b);
- Ft.replace (R.ID.F, F);
- }
- @Override
- public void ontabunselected (Tab tab, fragmenttransaction ft) {
- //TODO auto-generated method stub
- }
- @Override
- public void ontabreselected (Tab tab, fragmenttransaction ft) {
- //TODO auto-generated method stub
- }
- }
[Java]View PlainCopyprint?
- Public class Fragmentnew extends Fragment {
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container,
- Bundle savedinstancestate) {
- //TODO auto-generated method stub
- TextView t = new TextView (Getactivity ());
- int position = Getarguments (). GetInt ("position");
- switch (position) {
- Case 0:
- T.settext ("headline");
- Break ;
- case 1:
- T.settext ("Sport");
- Break ;
- Case 2:
- T.settext ("Finance");
- Break ;
- Case 3:
- T.settext ("car");
- Break ;
- }
- return t;
- }
- }
[HTML]View PlainCopyprint?
- <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="Http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingbottom="@dimen/activity_vertical_margin"
- android:paddingleft="@dimen/activity_horizontal_margin"
- android:paddingright="@dimen/activity_horizontal_margin"
- android:paddingtop="@dimen/activity_vertical_margin"
- tools:context=". Mainactivity " >
- <framelayout
- android:id="@+id/f"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </relativelayout>
Basic usage of Android--actionbar