This is the tab, and the container that puts the tab is tabhost.
How to achieve??
Each tab also corresponds to a layout, this is a bit of fun. An activity that corresponds to multiple functional layouts.
① Create a new tab item, note, do not generate main Activity
don't choose here .
② a new class Mytab in the package, inherited from Tabactivity
Actually, Tabactivity is a subclass of activity.
Package Zyf.tab.test;import android.app.TabActivity; Public class Mytab extends Tabactivity {}
③ inherit OnCreate () entry method from parent class
Package zyf.tab.test;import Android.app.tabactivity;import android.os.Bundle; Public class Mytab extends Tabactivity { @Override protectedvoid onCreate (Bundle Savedinstancestate) { // TODO auto-generated method stub Super.oncreate ( savedinstancestate);} }
④ Register the Mytab class (Activity) in the Manifest.xml file
<activity android:name=". Mytab"> <intent-filter> <action android:name=" Android.intent.action.MAIN"></action> <category android:name=" Android.intent.category.LAUNCHER"></category> </intent-filter></ Activity>
⑤ this time, you need to design the layout of the corresponding tab, the general use of Framelayout as the root layout, each label page corresponding to a child node layout
<?xml version="1.0"encoding="Utf-8"?><!--Here is the root node layout--><framelayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="fill_parent"android:layout_height="fill_parent"><!--The layout of the first tab--> <linearlayout android:id="@+id/widget_layout_blue"Android:layout_width="fill_parent"android:layout_height="fill_parent"androidrientation="Vertical"> <edittext android:id="@+id/widget34"Android:layout_width="fill_parent"Android:layout_height="wrap_content"android:text="EditText"android:textsize="18SP"> </EditText> <button android:id="@+id/widget30"Android:layout_width="wrap_content"Android:layout_height="wrap_content"android:text="Button"> </Button> </LinearLayout><!--The layout of the Second tab--> <linearlayout android:id="@+id/widget_layout_red"Android:layout_width="fill_parent"android:layout_height="fill_parent"androidrientation="Vertical"> <analogclock android:id="@+id/widget36"Android:layout_width="wrap_content"android:layout_height="wrap_content"> </AnalogClock> </LinearLayout><!--the Third tab layout--> <linearlayout android:id="@+id/widget_layout_green"Android:layout_width="fill_parent"android:layout_height="fill_parent"androidrientation="Vertical"> <radiogroup android:id="@+id/widget43"Android:layout_width="166px"android:layout_height="98px"androidrientation="Vertical"> <radiobutton android:id="@+id/widget44"Android:layout_width="wrap_content"android:layout_height="wrap_content"Android:text="RadioButton"> </RadioButton> <radiobutton android:id="@+id/widget45"Android:layout_width="wrap_content"android:layout_height="wrap_content"Android:text="RadioButton"> </RadioButton> </RadioGroup> </LinearLayout></FrameLayout>
⑥ First, you should declare tabhost, and then use Layoutinflater to filter out the layout to tabhost with the tab page Framelayout
Privatetabhost mytabhost;mytabhost= This. Gettabhost ();//get tab tabhost from TabactivityLayoutinflater. from( This). Inflate (R.layout.main, Mytabhost.gettabcontentview (),true);//from (this) get layoutinflater from the tabactivity//r.layout.main store Tab Layout//framelayout for storing tab content via Tabhost//whether to tie the inflate to the root layout elementMytabhost.setbackgroundcolor (Color.argb ( Max, A, -, Max));//set the color of the Tabhost
⑦ Next, create a label in Tabhost, then set the title/Icon/tab layout
mytabhost . AddTab (Mytabhost.newtabspec ("tt")// manufacture of a new label TT . Setindicator ("KK", getresources (). Getdrawable ( R.DRAWABLE.AJJC) // Set the caption for KK, set the label icon to AJJC . SetContent ( r.id.widget_layout_red)); // set the layout of the tab to R.id.widget_layout_red, which is a sub-layout in Framelayout
⑧ tag switching event handling, Setontabchangedlistener
Mytabhost.setontabchangedlistener (new Ontabchangelistener () { @Override public void ontabchanged (String tabId) { // TODO auto-generated method Stub } });
⑨ dynamic menu for each tab page
Put the menu that was designed in XML into an int array first
Private Static int mymenuresources[] = {r.menu.phonebook_menu, r.menu.addphone_menu, R.menu.chatting_menu, R.menu.userapp_menu};
Set the Mymenusettingtag in the Setontabchangedlistener () method according to the switch of the label
@Override Public voidontabchanged (String tagstring) {//TODO auto-generated Method Stub if(Tagstring.equals (" One") ) {Mymenusettingtag=1; } if(Tagstring.equals (" Both") ) {Mymenusettingtag=2; } if(Tagstring.equals ("three") ) {Mymenusettingtag=3; } if(Tagstring.equals (" Four") ) {Mymenusettingtag=4; } if(MyMenu! =NULL) {oncreateoptionsmenu (myMenu); } }
The menu is then dynamically added via the Menuinflater filter in the Oncreateoptionsmenu (Menu menu) method.
@Override Publicboolean Oncreateoptionsmenu (Menu menu) {//TODO auto-generated Method Stub// hold on to thisMyMenu =menu; Mymenu.clear ();//Clear Menu//Inflate the currently selected menu XML resource.Menuinflater Inflater =Getmenuinflater (); //get a menu filter from Tabactivity Switch(Mymenusettingtag) { Case 1: Inflater.inflate (mymenuresources[0], menu); //dynamically joins the corresponding XML menu in the array Break; Case 2: Inflater.inflate (mymenuresources[1], menu); Break; Case 3: Inflater.inflate (mymenuresources[2], menu); Break; Case 4: Inflater.inflate (mymenuresources[3], menu); Break; default: Break; } returnsuper.oncreateoptionsmenu (menu); }
⑩ Operation effect
Code Address: Http://pan.baidu.com/s/1ntE8vw9
Android tab with Tabhost