2010-06-07Android TabWidget/TabHost usage
Article category: Java programmingAndroid TabWidget/TabHost can be used in either of the following ways:
First, use the built-in TabHost (and inherit from the TabActivity class) Code as follows:
Java code
Bytes
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <LinearLayout android: id = "@ + id/tab1"
- Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
- Androidrientation = "vertical">
- <TextView android: id = "@ + id/TextView1"
- Android: text = "This is a tab1" android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android: id = "@ + id/tab2"
- Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
- Androidrientation = "vertical">
- <TextView android: id = "@ + id/TextView2"
- Android: text = "This is a tab2" android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android: id = "@ + id/tab3"
- Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
- Androidrientation = "vertical">
- <TextView android: id = "@ + id/TextView3"
- Android: text = "This is a tab3" android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- </TextView>
- </LinearLayout>
- </FrameLayout>
Java code
Bytes
- Package com. Aina. Android;
- Import android. app. AlertDialog;
- Import android. app. Dialog;
- Import android. app. TabActivity;
- Import android. content. DialogInterface;
- Import android. OS. Bundle;
- Import android. view. LayoutInflater;
- Import android. widget. TabHost;
- Public class Test_TabWidget extends TabActivity {
- /** Called when the activity is first created .*/
- Private TabHost tabHost;
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- // SetContentView (R. layout. main );
- TabHost = this. getTabHost ();
- LayoutInflater li = LayoutInflater. from (this );
- Li. inflate (R. layout. main, tabHost. getTabContentView (), true );
- TabHost. addTab (tabHost. newTabSpec ("Tab_1"). setContent (R. id. tab1)
- . SetIndicator ("TAB1 ",
- This. getResources (). getDrawable (R. drawable. img1 )));
- TabHost. addTab (tabHost. newTabSpec ("Tab_2"). setContent (R. id. tab2)
- . SetIndicator ("TAB2 ",
- This. getResources (). getDrawable (R. drawable. img2 )));
- TabHost. addTab (tabHost. newTabSpec ("Tab_3"). setContent (R. id. tab3)
- . SetIndicator ("TAB3 ",
- This. getResources (). getDrawable (R. drawable. img3 )));
- TabHost. setCurrentTab (1 );
- // TabHost. setBackgroundColor (Color. GRAY );
- TabHost. setOnTabChangedListener (new TabHost. OnTabChangeListener (){
- Public void onTabChanged (String tabId ){
- Dialog dialog = new AlertDialog. Builder (Test_TabWidget.this)
- . SetTitle ("prompt"). setMessage (
- "+ TabId +" tab "selected). setIcon (R. drawable. icon). setPositiveButton (" OK ", new DialogInterface. OnClickListener (){
- Public void onClick (DialogInterface dialog,
- Int which ){
- // TODO Auto-generated method stub
- }
- }). Create ();
- Dialog. show ();
- }
- });
- }
- }
Second, we define our own tabHost: we do not need to inherit TabActivity. The specific code is as follows:
Java code
Bytes
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <TabHost xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: id = "@ + id/TabHost01" android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <LinearLayout android: layout_width = "fill_parent"
- Android: orientation = "vertical" android: layout_height = "fill_parent">
- <TabWidget android: id = "@ android: id/tabs"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"/>
- <FrameLayout android: id = "@ android: id/tabcontent"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <LinearLayout android: id = "@ + id/LinearLayout1"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- <TextView android: text = "one"
- Android: id = "@ + id/TextView01" android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android: id = "@ + id/LinearLayout2"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content">
- <TextView android: text = "two"
- Android: id = "@ + id/TextView02" android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android: id = "@ + id/LinearLayout3"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content">
- <TextView android: text = "three"
- Android: id = "@ + id/TextView03" android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- </TextView>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
- </TabHost>
Java code
Bytes
- Package com. Aina. Android;
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. util. Log;
- Import android. view. LayoutInflater;
- Import android. widget. TabHost;
- Public class Test_TabHost extends Activity {
- /** Called when the activity is first created .*/
- Private TabHost tabHost;
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- Try {
- TabHost = (TabHost) this. findViewById (R. id. TabHost01 );
- TabHost. setup ();
- TabHost. addTab (tabHost. newTabSpec ("tab_1 ")
- . SetContent (R. id. LinearLayout1)
- . SetIndicator ("TAB1", this. getResources (). getDrawable (R. drawable. img1 )));
- TabHost. addTab (tabHost. newTabSpec ("tab_2 ")
- . SetContent (R. id. LinearLayout2). setIndicator ("TAB2 ",
- This. getResources (). getDrawable (R. drawable. img2 )));
- TabHost. addTab (tabHost. newTabSpec ("tab_3 ")
- . SetContent (R. id. LinearLayout3). setIndicator ("TAB3 ",
- This. getResources (). getDrawable (R. drawable. img3 )));
- TabHost. setCurrentTab (1 );
- } Catch (Exception ex ){
- Ex. printStackTrace ();
- Log. d ("EXCEPTION", ex. getMessage ());
- }
- }
- }
Note: the id of the TabWidget in the layout file must be defined in the second method.
Android: id = "@ android: id/tabs". The id of FrameLayout must be defined.
Android: id = "@ android: id/tabcontent" is not restricted. Otherwise, an error is returned.