1: Configure the contents of the tab in the layout file
<?xml version= "1.0" encoding= "Utf-8"?><tabhost xmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@android: Id/tabhost"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_weight= "1" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <Tabwidget Android:id= "@android: Id/tabs"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/> <framelayout Android:id= "@android: Id/tabcontent"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <!--define the contents of the first tab--<LinearLayout Android:id= "@+id/tab01"android:orientation= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Daughter Kingdom King-2012/12/12"android:textsize= "11pt"/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "East Women-2012/12/18"android:textsize= "11pt"/> </LinearLayout> <!--define the contents of the Second tab--<LinearLayout Android:id= "@+id/tab02"android:orientation= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Bones Jing-2012/08/12"android:textsize= "11pt"/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Spider Essence-2012/09/20"android:textsize= "11pt"/> </LinearLayout> <!--define the contents of the Third tab--<LinearLayout Android:id= "@+id/tab03"android:orientation= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:textsize= "11pt" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Monkey King-2012/09/19"android:textsize= "11pt"/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Pig-2012/10/12"android:textsize= "11pt"/> <Button Android:id= "@+id/btn"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "OK"/> </LinearLayout> </FrameLayout> </LinearLayout></TabHost>
Code of the response
PackageOrg.crazyit.ui;Importandroid.app.TabActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TabHost;ImportAndroid.widget.Toast;ImportAndroid.widget.TabHost.TabSpec;/*** Description: * <br/>site: <a href= "http://www.crazyit.org">crazyit.org</a> * <br/>copyright (C), 2001-2014, Yeeku.h.lee * <br/>this program is protected by copyright laws. * <br/>program Name: * <br/>date: *@authorYeeku.h.lee [email protected] *@version1.0*/ Public classTabhosttestextendstabactivity{@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //get the Tabhost component inside the activityTabhost Tabhost =Gettabhost (); //Create first tab pageTabspec tab1 = Tabhost.newtabspec ("Tab1"). Setindicator ("Answered phone")//Set Title. SetContent (R.ID.TAB01);//Set Content//Add first tab pageTabhost.addtab (TAB1); Tabspec tab2= Tabhost.newtabspec ("TaB2") //Place an icon on a label title. Setindicator ("Outgoing Call", Getresources (). getdrawable (R.drawable.ic_launcher)). SetContent (R.ID.TAB02); //Add a second tab pageTabhost.addtab (TAB2); Tabspec tab3= Tabhost.newtabspec ("Tab3"). Setindicator ("Missed Call"). SetContent (R.ID.TAB03); //Add a third tab pageTabhost.addtab (TAB3); Button btn=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (Getapplicationcontext (),"Toast", Toast.length_short). Show (); } }); }}
2: The contents of the tab are configured by different layout files
Main.xml
<?xml version= "1.0" encoding= "Utf-8"?><tabhost xmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@android: Id/tabhost"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_weight= "1" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <Tabwidget Android:id= "@android: Id/tabs"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/> <framelayout Android:id= "@android: Id/tabcontent"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > </FrameLayout> </LinearLayout></TabHost>
Set the activity of the tab
Packageorg.crazyit.intent;Importandroid.app.TabActivity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.widget.TabHost;ImportAndroid.widget.TabHost.TabSpec;/*** Description: * <br/>site: <a href= "http://www.crazyit.org">crazyit.org</a> * <br/>copyright (C), 2001-2014, Yeeku.h.lee * <br/>this program is protected by copyright laws. * <br/>program Name: * <br/>date: *@authorYeeku.h.lee [email protected] *@version1.0*/ Public classIntenttabextendstabactivity{@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //get the Tabhost component inside the activityTabhost Tabhost =Gettabhost (); //Add the first tab page using intent
Bacallactivity, callactivity,nocallactitity for three activity, controls the contents of the Display tab
Tabhost.addtab (tabhost. Newtabspec ("Tab1"). Setindicator ("Answered Phone", Getresources (). getdrawable (R.drawable.ic_launcher)). SetContent (NewIntent ( This, Becalledactivity.class))); //Add a second tab page using intentTabhost.addtab (Tabhost.newtabspec ("Tab1"). Setindicator ("Outgoing Call"). SetContent (NewIntent ( This, Calledactivity.class))); //Add a third tab page using intentTabhost.addtab (Tabhost.newtabspec ("Tab1"). Setindicator ("Missed Call"). SetContent (NewIntent ( This, Nocallactivity.class))); }}
Android Tabhost (tab)