Android 中的TabHost相當於VC或者Swing中的選項卡,本文中所提到的選項卡就代表TabHost。在Android中選項卡由TabActivity來實現,TabActivity是一個ActivityGroup,它持有TabHost物件,TabHost是一個View物件,它的基本構成如下面的佈局檔案所示

<?xml version=“1.0” encoding=“utf-8”?>

<TabHost xmlns:android=“http://schemas.android.com/apk/res/android”

android:id=“@android:id/tabhost” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

<LinearLayout android:orientation=“vertical”

android:layout_width=“fill_parent” android:layout_height=“fill_parent”>

<FrameLayout android:id=“@android:id/tabcontent”

android:layout_width=“fill_parent” android:layout_height=“wrap_content”

android:layout_weight=“1” />

<TabWidget android:id=“@android:id/tabs” android:layout_width=“fill_parent”

android:layout_height=“wrap_content” android:layout_weight=“0” android:gravity=“center” />

</LinearLayout>

   FrameLayout表示選項卡內容顯示的佈局,TabWidget標識選項卡標籤顯示的佈局

實際上一個TabHost包含一個FrameLayout和一個TabWidget,這個檔案是我參照系統自帶的tab_content檔案修改的,只是將TabWidget的位置和FrameLayout的位置調換了一下,目的是為了使選項卡的標籤顯示在螢幕的下方,如果你沒有自定義TabHost的佈局,預設情況下選項卡標籤是顯示在螢幕上方的。 對於選項卡的內容,可以有三種形式:

<1>最常用的來自其他Activity

   TabHost.TabSpec indexTab=myTabHost.newTabSpec(“index”);//建立選項卡標籤物件

   Intent  indexIntent=new Intent(current.this,Target.class);//選擇此標籤時,將要跳轉 的Activity,這個Activity就顯示在FrameLayout佈局中。

   indexTab.setIndicator(“標籤名”);//設定標籤名也可以設定這個標籤對應的圖片

   indexTab.setContent(indexIntent);//設定標籤的內容,

   myTabHost.addTab(indexTab);//將標籤新增到選項卡

<2>來自佈局檔案中的View,這個佈局檔案必須是TabHost的內容佈局

   LayoutInflater.from(this).inflate(R.layout.tab  myTabHost.getTabContentView(),true);

   TabHost.TabSpec indexTab=myTabHost.newTabSpec(“index”);

    indexTab.setIndicator(“標籤名”);

   indexTab.setContent(R.id.yourview);

   //yourview 必須在tab.xml中已經定義了,tab.xml必須是FrameLayout

   myTabHost.addTab(indexTab);

    myTabHost.addTab(indexTab);

<3>來自通過Java程式碼實現的View

     TabHost.TabSpec indexTab=myTabHost.newTabSpec(“index”);

    indexTab.setIndicator(“標籤名”);

   indexTab.setContent(new TabHost.TabContentFactory()

                            {

                             public View createTabContent(String tag) {

                             TextView myText=new TextView(currentActivity.this);

                             myText.setText(” 標籤名”);

                             Return myText;

                             }

                            }

);

 myTabHost.addTab(indexTab);

     Android系統中自帶的tab_content.xml實際上和TabHost物件是對等的。通過分析

原始碼就可以得出,TabActivity通過getTabHost()方法,就將ContentView設定為tab_content

如果不通過getTabHost()方法來初始化TabHost物件,為了簡單起見,自己構造的xml檔案也必須和tab_content.xml中定義的類似,就像我自己改的那樣。View 的id必須為系統自帶的id,如android:id/tabhost,因為TabActivity中很多方法的判斷都是根據系統自帶的id得出的。如果需要構造完全自定義的TabHost,就必須重寫很多TabActivity的方法。如果只是將TabActivity用作一般UI顯示,完全沒有這個必要。

    具體實現如下:

第一步:讓自己的Activity作為TabActivity的子類,持有一個TabHost物件myTabHost

第二步:實現自己的TabHost,編寫對應的佈局檔案。如果只是通過getTabHost()來初始化TabHost(),可以寫 myTabHost=getTabHost(),如果要通過自定義佈局檔案來實現,

myTabHost=(TabHost)findViewById(android.R.id.tabhost);

第三步:如果需要改變TabHost內容區域的佈局

LayoutInflater.from(this).inflate(R.layout.mainmyTabHost.getTabContentView());

如果選項卡內容需用到main.xml中的View物件,就必須加上這一句,如果沒有用到,就可以不寫。

第四步:新增選項卡內容

TabHost.TabSpec indexTab=myTabHost.newTabSpec(“index”);

Intent indexIntent=new Intent(MicroBlogMain.this,MicroBlogIndex.class);

indexTab.setIndicator(getString(R.string.index));//也可以設定圖示

indexTab.setContent(indexIntent);

myTabHost.addTab(indexTab);

myTabHost.setup();