Android TabHost(标签)应用举例


为TabHost的运行结果:在一个Activity里面显示不同的标签里的内容。图在最后

代码如下:

AppMain.java

  1. package com.lxy;  
  2.   
  3. import Android.app.TabActivity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.TabHost;  
  10.   
  11. public class AppMain extends TabActivity {  
  12.   
  13.     private TabHost myTabHost;  
  14.     private Button bt01;  
  15.     private Button bt02;  
  16.     private Button bt03;  
  17.     private Intent it01;  
  18.     private Intent it02;  
  19.     private Intent it03;  
  20.       
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.           
  25.         initWidget();  
  26.         initIntent();  
  27.     }  
  28.       
  29.     private void initWidget(){  
  30.         bt01 = (Button)findViewById(R.id.tab_ib_service);  
  31.         bt02 = (Button)findViewById(R.id.tab_ib_history);  
  32.         bt03 = (Button)findViewById(R.id.tab_ib_eixt);  
  33.           
  34.         bt01.setOnClickListener(new ButtonListener());  
  35.         bt02.setOnClickListener(new ButtonListener());  
  36.         bt03.setOnClickListener(new ButtonListener());  
  37.           
  38.         it01 = new Intent(this,Test01.class);  
  39.         it02 = new Intent(this,Test02.class);  
  40.         it03 = new Intent(this,Test03.class);  
  41.     }  
  42.       
  43.     class ButtonListener implements OnClickListener{  
  44.         public void onClick(View v) {  
  45.             if(v.getId() == R.id.tab_ib_service){  
  46.                 myTabHost.setCurrentTabByTag("t1");  
  47.             } else if(v.getId() == R.id.tab_ib_history){  
  48.                 myTabHost.setCurrentTabByTag("t2");  
  49.             } else if(v.getId() == R.id.tab_ib_eixt){  
  50.                 myTabHost.setCurrentTabByTag("t3");  
  51.             }  
  52.         }  
  53.     }  
  54.       
  55.     private void initIntent(){  
  56.         this.myTabHost = getTabHost();  
  57.         TabHost localTabHost = this.myTabHost;  
  58.           
  59.         // newTabSpec()     --->  Get a new TabHost.TabSpec associated with this tab host.   
  60.         // setIndicator()   --->  Specify a label and icon as the tab indicator.   
  61.         // setContent()     --->  Specify an intent to use to launch an activity as the tab content.   
  62.         localTabHost.addTab(  
  63.                 myTabHost.newTabSpec("t1")  
  64.                 .setIndicator("LIU", getResources().getDrawable(R.drawable.icon))  
  65.                 .setContent(it01));  
  66.         localTabHost.addTab(  
  67.                 myTabHost.newTabSpec("t2")  
  68.                 .setIndicator("ZHEN", getResources().getDrawable(R.drawable.icon))  
  69.                 .setContent(it02));  
  70.         localTabHost.addTab(  
  71.                 myTabHost.newTabSpec("t3")  
  72.                 .setIndicator("WEI", getResources().getDrawable(R.drawable.icon))  
  73.                 .setContent(it03));  
  74.     }  
  75. }  
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <LinearLayout android:orientation="vertical"  
  5.         android:layout_width="fill_parent" android:layout_height="fill_parent">  
  6.         <FrameLayout android:id="@android:id/tabcontent"  
  7.             android:layout_width="fill_parent" android:layout_height="1.0dip"  
  8.             android:layout_weight="1.0">  
  9.         </FrameLayout>  
  10.       
  11.         <TabWidget android:id="@android:id/tabs" android:visibility="gone"  
  12.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  13.             android:layout_weight="1.0" />  
  14.   
  15.         <LinearLayout android:orientation="horizontal"  
  16.             android:layout_width="fill_parent" android:layout_height="70px">  
  17.             <Button android:id="@+id/tab_ib_service"  
  18.                 android:layout_height="70px" android:layout_width="fill_parent"  
  19.                 android:layout_weight="1" android:text="Tab1"  
  20.                 android:scaleType="fitXY" >  
  21.             </Button>  
  22.             <Button android:id="@+id/tab_ib_history"  
  23.                 android:layout_height="70px" android:layout_width="fill_parent"  
  24.                 android:layout_weight="1" android:text="Tab2"  
  25.                 android:scaleType="fitXY"></Button>  
  26.             <Button android:id="@+id/tab_ib_eixt"  
  27.                 android:layout_height="70px" android:layout_width="fill_parent"  
  28.                 android:layout_weight="1" android:text="Tab3"  
  29.                 android:scaleType="fitXY">  
  30.             </Button></LinearLayout>  
  31.               
  32.     </LinearLayout>  
  33. </TabHost>  
Text01.java
  1. package com.lxy;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class Test01 extends Activity{  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.test01);  
  10.     }  
  11. }  
test01.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:background="#FF0000FF"  
  11.         android:text="TEST01"></TextView>  
  12.           
  13. </LinearLayout>  

Text02.java, Text03.java与Text.01.java 基本一样,只是加载自己对应的布局文件

而布局文件text02.xml, text03.xml与text01.xml基本一样,只是文本中显示的内容不同,背景颜色不同(当然也可以相同)。

相关内容