To use a tab:
1. Use framelayout in the layout file to list the tab component and the content component in the tab.
2. The activity must inherit tabactivity.
3. Call the gettabhost () method of tabactivity to obtain the tabhost object.
4. Use tabhost to create a tab.
/Chapter04_ui_tab01/src/COM/Amaker/test/mainactivity. Java
Code
Package Com. Amaker. test;
Import Android. App. tabactivity;
Import Android. OS. Bundle;
Import Android. View. layoutinflater;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. widget. tabhost;
Import Android. widget. Toast;
Import Android. widget. tabhost. ontabchangelistener;
Public Class Mainactivity Extends Tabactivity {
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
/* Requestwindowfeature (window. feature_no_title );
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
Windowmanager. layoutparams. flag_fullscreen ); */
Tabhost th = Gettabhost ();
Layoutinflater. From ( This ). Inflate (R. layout. Main, Th. gettabcontentview (), True );
Th. addtab (Th. newtabspec ( " All " ). Setindicator ( " All call records " ). Setcontent (R. Id. textview01 ));
Th. addtab (Th. newtabspec ( " OK " ). Setindicator ( " Received call " ). Setcontent (R. Id. textview02 ));
Th. addtab (Th. newtabspec ( " Cancel " ). Setindicator ( " Missed call " ). Setcontent (R. Id. textview03 ));
Th. setontabchangedlistener (
New Ontabchangelistener (){
@ Override
Public Void Ontabchanged (string Tabid ){
Toast. maketext (mainactivity. This , Tabid, Toast. length_long). Show ();
}
}
);
}
}
/Chapter04_ui_tab01/RES/layout/Main. xml
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Framelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: ID = "@ + ID/framelayout01"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content" >
< Textview
Android: ID = "@ + ID/textview01"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: Text = "All call records" > </ Textview >
< Textview
Android: ID = "@ + ID/textview02"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: Text = "Incoming calls" > </ Textview >
< Textview
Android: ID = "@ + ID/textview03"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: Text = "Missed calls" > </ Textview >
</ Framelayout >
Implement the createtabcontent method of tabhost. tabcontentfactory to define the tab content.
/Chapter04_ui_tab02/src/COM/Amaker/test/mainactivity. Java
Code
Package Com. Amaker. test;
Import Java. util. arraylist;
Import Java. util. List;
Import Android. App. tabactivity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. arrayadapter;
Import Android. widget. listview;
Import Android. widget. tabhost;
Public Class Mainactivity Extends Tabactivity Implements
Tabhost. tabcontentfactory {
/** Called when the activity is first created. */
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Tabhost th = Gettabhost ();
Th. addtab (Th. newtabspec ( " All " ). Setindicator ( " All call records " ). Setcontent ( This ));
Th. addtab (Th. newtabspec ( " OK " ). Setindicator ( " Received call " ). Setcontent ( This ));
Th
. Addtab (Th. newtabspec ( " Cancel " ). Setindicator ( " Missed call " )
. Setcontent ( This ));
}
Public View createtabcontent (string tag ){
Listview LV = New Listview ( This );
List < String > List = New Arraylist < String > ();
List. Add (TAG );
If (Tag. Equals ( " All " )){
List. Add ( " Tom " );
List. Add ( " Kite " );
List. Add ( " Rose " );
} Else If (Tag. Equals ( " OK " )){
List. Add ( " Tom " );
List. Add ( " Kite " );
} Else {
List. Add ( " Rose " );
}
Arrayadapter Adapter = New Arrayadapter ( This ,
Android. R. layout. simple_list_item_checked, list );
LV. setadapter (adapter );
Return LV;
}
}
/Chapter04_ui_tab02/RES/layout/Main. xml
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: Orientation = "Vertical"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
>
< Textview
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: Text = "@ String/hello"
/>
</ Linearlayout >