Android TabWidget/TabHost有两种使用方法:

第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:
 
Java代码
  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>
     
  30.  
复制代码

Java代码
  1. package com.Aina.Android;
     

  2.  
  3. import android.app.AlertDialog;
     
  4. import android.app.Dialog;
     
  5. import android.app.TabActivity;
     
  6. import android.content.DialogInterface;
     
  7. import android.os.Bundle;
     
  8. import android.view.LayoutInflater;
     
  9. import android.widget.TabHost;
     

  10.  
  11. public class Test_TabWidget extends TabActivity {
     
  12.         /** Called when the activity is first created. */
     
  13.         private TabHost tabHost;
     

  14.  
  15.         @Override
     
  16.         public void onCreate(Bundle savedInstanceState) {
     
  17.                 super.onCreate(savedInstanceState);
     
  18.                 // setContentView(R.layout.main);
     
  19.                 tabHost = this.getTabHost();
     
  20.                 LayoutInflater li = LayoutInflater.from(this);
     
  21.                 li.inflate(R.layout.main, tabHost.getTabContentView(), true);
     
  22.                 tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1)
     
  23.                                 .setIndicator("TAB1",
     
  24.                                                 this.getResources().getDrawable(R.drawable.img1)));
     
  25.                 tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2)
     
  26.                                 .setIndicator("TAB2",
     
  27.                                                 this.getResources().getDrawable(R.drawable.img2)));
     
  28.                 tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3)
     
  29.                                 .setIndicator("TAB3",
     
  30.                                                 this.getResources().getDrawable(R.drawable.img3)));
     
  31.                 tabHost.setCurrentTab(1);
     
  32. //                tabHost.setBackgroundColor(Color.GRAY);
     
  33.                 tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
     

  34.  
  35.                         public void onTabChanged(String tabId) {
     
  36.                                 Dialog dialog = new AlertDialog.Builder(Test_TabWidget.this)
     
  37.                                                 .setTitle("提示").setMessage(
     
  38.                                                                 "选中了" + tabId + "选项卡").setIcon(R.drawable.icon).setPositiveButton("确定", new DialogInterface.OnClickListener(){
     

  39.  
  40.   public void onClick(DialogInterface dialog,
     
  41.           int which) {
     
  42.            // TODO Auto-generated method stub
     
  43.                                                                                 
     
  44.           }
     
  45.                                                                         
     
  46.             }).create();
     
  47.              dialog.show();
     

  48.  
  49.                }
     

  50.  
  51.              });
     
  52.        }
     
  53. }
     
  54.  
复制代码

第二种:就是定义我们自己的tabHost:不用继承TabActivity,具体代码如下:
 
Java代码
  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>
     
  40.  
复制代码

Java代码
  1. package com.Aina.Android;
     

  2.  
  3. import android.app.Activity;
     
  4. import android.os.Bundle;
     
  5. import android.util.Log;
     
  6. import android.view.LayoutInflater;
     
  7. import android.widget.TabHost;
     

  8.  
  9. public class Test_TabHost extends Activity {
     
  10.         /** Called when the activity is first created. */
     
  11.         private TabHost tabHost;
     

  12.  
  13.         @Override
     
  14.         public void onCreate(Bundle savedInstanceState) {
     
  15.                 super.onCreate(savedInstanceState);
     
  16.                 setContentView(R.layout.main);
     
  17.                 try{
     
  18.                         tabHost = (TabHost) this.findViewById(R.id.TabHost01);
     
  19.                         tabHost.setup();
     
  20.                         
     
  21.                         tabHost.addTab(tabHost.newTabSpec("tab_1")
     
  22.                                         .setContent(R.id.LinearLayout1)
     
  23.                                         .setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img1)));
     
  24.                         tabHost.addTab(tabHost.newTabSpec("tab_2")
     
  25.                                         .setContent(R.id.LinearLayout2).setIndicator("TAB2",
     
  26.                                                         this.getResources().getDrawable(R.drawable.img2)));
     
  27.                         tabHost.addTab(tabHost.newTabSpec("tab_3")
     
  28.                                         .setContent(R.id.LinearLayout3).setIndicator("TAB3",
     
  29.                                                         this.getResources().getDrawable(R.drawable.img3)));
     
  30.                         tabHost.setCurrentTab(1);
     
  31.                 }catch(Exception ex){
     
  32.                         ex.printStackTrace();
     
  33.                         Log.d("EXCEPTION", ex.getMessage());
     
  34.                 }
     

  35.  
  36.         }
     
  37. }
     
  38.  
复制代码

注意:第二种方法时布局文件中的TabWidget的id必须定义为:android:id="@android:id/tabs",FrameLayout的id必须定义为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。

 

 

  1.当我们继承自一个TabActivity之后,就可以通过getTabHost()方法得到一个TabHost对象,接着再通过该对象得到TabWidget对象。

  

Java代码 复制代码  收藏代码
  1. final TabHost tabs = getTabHost();   
  2. final TabWidget tabWidget = tabs.getTabWidget();  

final TabHost tabs = getTabHost();
final TabWidget tabWidget = tabs.getTabWidget();

  

  2.接下来改写tabWidget中的各个标签对象的属性值,进而实现“定制”的功能,这里用到的就是Java的反射机制。

 

 

Java代码 复制代码  收藏代码
  1. for (int i = 0; i < tabWidget.getChildCount(); i++) {   
  2.             /**  
  3.              * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果  
  4.              */  
  5.             tabWidget.getChildAt(i).getLayoutParams().height = height;   
  6.             tabWidget.getChildAt(i).getLayoutParams().width = width;   
  7.   
  8.             /**  
  9.              * 设置tab中标题文字的颜色,不然默认为黑色  
  10.              */  
  11.             final TextView tv = (TextView) tabWidget.getChildAt(i)   
  12.                     .findViewById(android.R.id.title);   
  13.   
  14.             tv.setTextColor(this.getResources().getColorStateList(   
  15.                     android.R.color.white));   
  16.   
  17.             /**  
  18.              * 此方法是为了去掉系统默认的色白的底角  
  19.              *   
  20.              * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip  
  21.              * 都是私有变量,但是我们可以通过反射来获取  
  22.              *   
  23.              * 由于还不知道Android 2.2的接口,在这里先加个判断,避免报错  
  24.              */  
  25.             Log.d("debug""version is "+Build.VERSION.RELEASE);   
  26.             // 版本信息有的较长,比如2.2.3,2.1.3.1,我们只需要取前面的三位即可。   
  27.             final String VERSION = Build.VERSION.RELEASE.substring(03);   
  28.             Log.d("debug""version is "+VERSION);   
  29.             if (Float.valueOf(VERSION) <= 2.1) {   
  30.                 try {   
  31.                     Class<TabWidget> tabWidgetClass = (Class<TabWidget>) tabWidget.getClass();   
  32.                     mBottomLeftStrip = tabWidgetClass.getDeclaredField("mBottomLeftStrip");   
  33.                     mBottomRightStrip = tabWidgetClass.getDeclaredField("mBottomRightStrip");   
  34.                     // 判断是否有访问权限的控制   
  35.                     if (!mBottomLeftStrip.isAccessible()) {   
  36.                         // 取消访问权限控制   
  37.                         mBottomLeftStrip.setAccessible(true);   
  38.                     }   
  39.                     if (!mBottomRightStrip.isAccessible()) {   
  40.                         mBottomRightStrip.setAccessible(true);   
  41.                     }   
  42.                     // 设置属性值   
  43.                     mBottomLeftStrip.set(tabWidget,   
  44.                             getResources().getDrawable(R.drawable.alpha_00));   
  45.                     mBottomRightStrip.set(tabWidget, getResources()   
  46.                             .getDrawable(R.drawable.alpha_00));   
  47.   
  48.                 } catch (Exception e) {   
  49.                     e.printStackTrace();   
  50.                 }   
  51.             } else {   
  52.                 /**  
  53.                  * 不做任何处理  
  54.                  */  
  55.             }   
  56.             View vvv = tabWidget.getChildAt(i);   
  57.             if (tabs.getCurrentTab() == i) {   
  58.                 vvv.setBackgroundDrawable(getResources().getDrawable(   
  59.                         R.drawable.tab_enabled));   
  60.             } else {   
  61.                 vvv.setBackgroundDrawable(getResources().getDrawable(   
  62.                         R.drawable.tab_selected));   
  63.             }   
  64.   
  65.         }