Android的Tab控件(一)

Tab控件即標籤頁,可以在一頁中切換顯示n頁內容,要使用此效果,需要用到TabHost和Tabwidget類。

Tab控件具有兩種實現過程,一是在同一個Activity中切換顯示不同的標籤頁,二是每個標籤頁都由獨立的Activity實現。我們首先用第二種方法來實現。


新建一個TabHostActivity,它繼承自TabActivity,

public class TabHostActivity extends TabActivity{};


他的佈局文件tabhostlayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@android:id/tabhost" > <!--注意這裏,id的設置方法跟普通控件不同,必須爲tabhost -->
    
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        
        <TabWidget <!--TabWidget控件和FrameLayout控件是必須的,分別用來表示標籤和標籤下面的內容,同樣注意其id選項的設置 -->
        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="wrap_content" />
            
       
    </LinearLayout>
    
</TabHost>


 TabHostActivity類的源碼:

public class TabHostActivity extends TabActivity{


Intent intent;
TabHost.TabSpec tabSpec;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhostlayout);

Resources res=getResources();

TabHost tabHost=getTabHost();

intent = new Intent(TabHostActivity.this,Tab1Activity.class);
tabSpec = tabHost.newTabSpec("tab1"); //創建一個新的標籤頁,標記爲“tab1”
tabSpec.setIndicator("tab1", res.getDrawable(R.drawable.ic_launcher));//設置tab頁的名稱和圖像表示
tabSpec.setContent(intent);//設置此tab跳轉到的Activity
tabHost.addTab(tabSpec);//將此tab加入到tabHost

intent = new Intent(TabHostActivity.this,Tab2Activity.class);
tabSpec=tabHost.newTabSpec("tab2");
tabSpec.setIndicator("tab2", res.getDrawable(R.drawable.ic_launcher));
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);

tabHost.setCurrentTab(1);//設置當期的tab頁,從0開始偏移
}


}


然後新建Tab1Activity類,併爲其建立佈局文件tab1layout.xml,在其中實現tab1頁的顯示內容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="this is in tab1 !" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK" />
</LinearLayout>


同樣新建Tab2Activity類,併爲其建立佈局文件tab2layout.xml,在其中實現tab2頁的顯示內容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <EditText 
        android:id="@+id/editText"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK" />
</LinearLayout>

將以上三個Activity添加到manifest文件中即可。


另外一種方法中,各tab都在一個Activity中,在下一篇中進行介紹。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章