This example for you to share the Actionbar to achieve the tab navigation effect of the specific code for your reference, the specific contents are as follows
First, a basic knowledge:
First, the basic use of methods
1. Get Actionbar
Getting Actionbar is very simple, in activity, Ationbar=this.getactionbar ();
2. Show/Hide
Set Actionbar Display/hide, you can use the show () and Hide () methods.
3. Set MenuItem
By setting MenuItem, you can make MenuItem the item on Actionbar.
setshowasaction (int actionenum), this actionenum supports the following parameters:
Show_as_action_always: Always display the MenuItem on the Actionbar
Show_as_action_collapse_action_view: Fold the actionview into a normal menu item
Show_as_action_if_room: When the actionbar position is sufficient, it is displayed on the Actionbar.
Show_as_action_never: The MenuItem is not displayed on the Actionbar.
Show_as_action_with_text: Displays the MenuItem on the Actionbar and displays the text of the menu item.
You can also set the property android:showasaction of the item in the XML attribute.
4. Enable program icon Navigation
Setdisplayhomeasupenabled (Boolean Showhomeasup): Sets whether the application icon should be turned into a clickable icon, and add a left arrow to the icon.
setdisplayoptions (int options): Controls the display options for Actionbar. The Opitions option is:
Display_home_as_up
Display_show_custom
Display_show_home
Display_show_title
Display_use_logo
Navigation_mode_list
Navigation_mode_standard
Navigation_mode_tabs
Setdislayshowhomeenabled (Boolean Showhome): Sets whether the icon for the application is displayed.
Sethomebuttonenabled (Boolean eabled): Sets whether to turn the application icon into a clickable button.
Add View in 5.actionbar
When you define the action item, use the Android:actionviewclass property to specify the implementation class for the action view.
When you define the action item, use the Android:actionlayout property to specify the view resource for the action view.
Second, Android Smart with Actionbar to achieve tab navigation effect
the use of Actionbar can also easily achieve the tab navigation effect, with the use of fragment to achieve switching between view functions.
If you want to use this function, you need
1) Set Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs) to enable Actionbar to use the tab navigation function.
2 Call the Actionbar AddTab () method, add multiple tab tags, and add a time listener for each tab tag.
Myfragment.java
Package com.app.main;
Import Android.annotation.SuppressLint;
Import android.app.Fragment;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.ViewGroup.LayoutParams;
Import Android.widget.TextView;
@SuppressLint ("Newapi") public
class Myfragment extends Fragment {
@Override public
View Oncreateview ( Layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {context Context
= This.getactivity ();
TextView TV = new TextView (context);
Bundle arc = this.getarguments ();
int Tabs=arc.getint ("key");
Tv.setlayoutparams (New Layoutparams (layoutparams.fill_parent,
layoutparams.wrap_content));
Tv.settext ("Hello Actionbar" +tabs);
return TV;
}
}
Main.xml
<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 "
tools:context= ". Main ">
<linearlayout
android:id=" @+id/container "
android:layout_width=" Match_parent
" android:layout_height= "Match_parent"
android:orientation= "vertical"/>
</RelativeLayout>
Main.java
Package com.app.main;
Import Android.app.ActionBar;
Import Android.app.ActionBar.Tab;
Import android.app.Activity;
Import android.app.FragmentTransaction;
Import Android.os.Bundle;
public class Main extends activity implements Actionbar.tablistener {Actionbar actionbar = null;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Actionbar = This.getactionbar ();
Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs);
Actionbar.addtab (Actionbar.newtab (). SetText ("Tab1"). Settablistener (this);
Actionbar.addtab (Actionbar.newtab (). SetText ("TaB2"). Settablistener (this);
Actionbar.addtab (Actionbar.newtab (). SetText ("Tab3"). Settablistener (this); @Override public void ontabreselected (Tab tab, fragmenttransaction ft) {} @Override the public void ontabselected (T
AB tab, fragmenttransaction ft) {myfragment Frag = new Myfragment (); int index = tab.getposition ()+ 1;
Bundle Bundle = new Bundle ();
Bundle.putint ("key", index);
Frag.setarguments (bundle);
Fragmenttransaction action = Main.this.getFragmentManager (). BeginTransaction ();
Action.replace (R.id.container, Frag);
Action.commit ();
@Override public void ontabunselected (Tab tab, fragmenttransaction ft) {}}
Implementation effect:
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.