Use of Android TabWidget/TabHost

Source: Internet
Author: User
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
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent">
  5. <LinearLayout android: id = "@ + id/tab1"
  6. Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
  7. Androidrientation = "vertical">
  8. <TextView android: id = "@ + id/TextView1"
  9. Android: text = "This is a tab1" android: layout_width = "fill_parent"
  10. Android: layout_height = "wrap_content">
  11. </TextView>
  12. </LinearLayout>
  13. <LinearLayout android: id = "@ + id/tab2"
  14. Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
  15. Androidrientation = "vertical">
  16. <TextView android: id = "@ + id/TextView2"
  17. Android: text = "This is a tab2" android: layout_width = "fill_parent"
  18. Android: layout_height = "wrap_content">
  19. </TextView>
  20. </LinearLayout>
  21. <LinearLayout android: id = "@ + id/tab3"
  22. Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
  23. Androidrientation = "vertical">
  24. <TextView android: id = "@ + id/TextView3"
  25. Android: text = "This is a tab3" android: layout_width = "fill_parent"
  26. Android: layout_height = "wrap_content">
  27. </TextView>
  28. </LinearLayout>
  29. </FrameLayout>
Java code

Bytes
  1. Package com. Aina. Android;
  2. Import android. app. AlertDialog;
  3. Import android. app. Dialog;
  4. Import android. app. TabActivity;
  5. Import android. content. DialogInterface;
  6. Import android. OS. Bundle;
  7. Import android. view. LayoutInflater;
  8. Import android. widget. TabHost;
  9. Public class Test_TabWidget extends TabActivity {
  10. /** Called when the activity is first created .*/
  11. Private TabHost tabHost;
  12. @ Override
  13. Public void onCreate (Bundle savedInstanceState ){
  14. Super. onCreate (savedInstanceState );
  15. // SetContentView (R. layout. main );
  16. TabHost = this. getTabHost ();
  17. LayoutInflater li = LayoutInflater. from (this );
  18. Li. inflate (R. layout. main, tabHost. getTabContentView (), true );
  19. TabHost. addTab (tabHost. newTabSpec ("Tab_1"). setContent (R. id. tab1)
  20. . SetIndicator ("TAB1 ",
  21. This. getResources (). getDrawable (R. drawable. img1 )));
  22. TabHost. addTab (tabHost. newTabSpec ("Tab_2"). setContent (R. id. tab2)
  23. . SetIndicator ("TAB2 ",
  24. This. getResources (). getDrawable (R. drawable. img2 )));
  25. TabHost. addTab (tabHost. newTabSpec ("Tab_3"). setContent (R. id. tab3)
  26. . SetIndicator ("TAB3 ",
  27. This. getResources (). getDrawable (R. drawable. img3 )));
  28. TabHost. setCurrentTab (1 );
  29. // TabHost. setBackgroundColor (Color. GRAY );
  30. TabHost. setOnTabChangedListener (new TabHost. OnTabChangeListener (){
  31. Public void onTabChanged (String tabId ){
  32. Dialog dialog = new AlertDialog. Builder (Test_TabWidget.this)
  33. . SetTitle ("prompt"). setMessage (
  34. "+ TabId +" tab "selected). setIcon (R. drawable. icon). setPositiveButton (" OK ", new DialogInterface. OnClickListener (){
  35. Public void onClick (DialogInterface dialog,
  36. Int which ){
  37. // TODO Auto-generated method stub
  38. }
  39. }). Create ();
  40. Dialog. show ();
  41. }
  42. });
  43. }
  44. }

Second, we define our own tabHost: we do not need to inherit TabActivity. The specific code is as follows:

Java code

Bytes
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <TabHost xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: id = "@ + id/TabHost01" android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent">
  5. <LinearLayout android: layout_width = "fill_parent"
  6. Android: orientation = "vertical" android: layout_height = "fill_parent">
  7. <TabWidget android: id = "@ android: id/tabs"
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "wrap_content"/>
  10. <FrameLayout android: id = "@ android: id/tabcontent"
  11. Android: layout_width = "fill_parent"
  12. Android: layout_height = "fill_parent">
  13. <LinearLayout android: id = "@ + id/LinearLayout1"
  14. Android: layout_width = "fill_parent"
  15. Android: layout_height = "wrap_content">
  16. <TextView android: text = "one"
  17. Android: id = "@ + id/TextView01" android: layout_width = "wrap_content"
  18. Android: layout_height = "wrap_content">
  19. </TextView>
  20. </LinearLayout>
  21. <LinearLayout android: id = "@ + id/LinearLayout2"
  22. Android: layout_width = "wrap_content"
  23. Android: layout_height = "wrap_content">
  24. <TextView android: text = "two"
  25. Android: id = "@ + id/TextView02" android: layout_width = "fill_parent"
  26. Android: layout_height = "wrap_content">
  27. </TextView>
  28. </LinearLayout>
  29. <LinearLayout android: id = "@ + id/LinearLayout3"
  30. Android: layout_width = "wrap_content"
  31. Android: layout_height = "wrap_content">
  32. <TextView android: text = "three"
  33. Android: id = "@ + id/TextView03" android: layout_width = "fill_parent"
  34. Android: layout_height = "wrap_content">
  35. </TextView>
  36. </LinearLayout>
  37. </FrameLayout>
  38. </LinearLayout>
  39. </TabHost>
Java code

Bytes
  1. Package com. Aina. Android;
  2. Import android. app. Activity;
  3. Import android. OS. Bundle;
  4. Import android. util. Log;
  5. Import android. view. LayoutInflater;
  6. Import android. widget. TabHost;
  7. Public class Test_TabHost extends Activity {
  8. /** Called when the activity is first created .*/
  9. Private TabHost tabHost;
  10. @ Override
  11. Public void onCreate (Bundle savedInstanceState ){
  12. Super. onCreate (savedInstanceState );
  13. SetContentView (R. layout. main );
  14. Try {
  15. TabHost = (TabHost) this. findViewById (R. id. TabHost01 );
  16. TabHost. setup ();
  17. TabHost. addTab (tabHost. newTabSpec ("tab_1 ")
  18. . SetContent (R. id. LinearLayout1)
  19. . SetIndicator ("TAB1", this. getResources (). getDrawable (R. drawable. img1 )));
  20. TabHost. addTab (tabHost. newTabSpec ("tab_2 ")
  21. . SetContent (R. id. LinearLayout2). setIndicator ("TAB2 ",
  22. This. getResources (). getDrawable (R. drawable. img2 )));
  23. TabHost. addTab (tabHost. newTabSpec ("tab_3 ")
  24. . SetContent (R. id. LinearLayout3). setIndicator ("TAB3 ",
  25. This. getResources (). getDrawable (R. drawable. img3 )));
  26. TabHost. setCurrentTab (1 );
  27. } Catch (Exception ex ){
  28. Ex. printStackTrace ();
  29. Log. d ("EXCEPTION", ex. getMessage ());
  30. }
  31. }
  32. }

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.