總結Android中TabActivity 的使用方法

轉自:http://www.itivy.com/android/archive/2011/6/27/android-tabactivity-usage.html

如果希望在Activity中出現多個Tab可以點擊,並且點擊每個Tab之後跳轉到相應的Activity,可以使用TabActivity類。以下演示一個簡單的範例。

首先要定義一個繼承TabActivity的類,這裏我們定義MainActivity,並且使其作爲應用程序的入口。其代碼爲

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.myandroid.tabtest; 
   
import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 
   
public class MainActivity extends TabActivity { 
    private TabHost m_tabHost; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.tabs); 
           
        //getTabHost返回的TabHost用於裝載tabs 
        m_tabHost = getTabHost(); 
           
        //add tabs,這裏用於添加具體的Tabs,並用Tab觸發相應的Activity 
        addOneTab(); 
        addTwoTab(); 
        addThreeTab(); 
        addFourTab(); 
    
       
    public void addOneTab(){ 
        Intent intent = new Intent(); 
        intent.setClass(MainActivity.this, OneActivity.class); 
           
        TabSpec spec = m_tabHost.newTabSpec("One"); 
        spec.setIndicator(getString(R.string.one), null); 
        spec.setContent(intent);         
        m_tabHost.addTab(spec); 
    
       
    public void addTwoTab(){ 
        Intent intent = new Intent(); 
        intent.setClass(MainActivity.this, TwoActivity.class); 
           
        TabSpec spec = m_tabHost.newTabSpec("Two"); 
        spec.setIndicator(getString(R.string.two), null); 
        spec.setContent(intent);         
        m_tabHost.addTab(spec); 
    
    public void addThreeTab(){ 
        Intent intent = new Intent(); 
        intent.setClass(MainActivity.this, ThreeActivity.class); 
           
        TabSpec spec = m_tabHost.newTabSpec("Three"); 
        spec.setIndicator(getString(R.string.three), null); 
        spec.setContent(intent);         
        m_tabHost.addTab(spec); 
    
    public void addFourTab(){ 
        Intent intent = new Intent(); 
        intent.setClass(MainActivity.this, FourActivity.class); 
           
        TabSpec spec = m_tabHost.newTabSpec("Four"); 
        spec.setIndicator(getString(R.string.four), null); 
        spec.setContent(intent);         
        m_tabHost.addTab(spec); 
    
}

可以看到在MainActivity中,我們使用getTabHost()返回一個TabHost,而TabHost正是用來添加Tabs的。這裏 我們添加了4個Tabs,使用4個函數完成:addOneTab(),addTwoTab(),addThreeTab(),addFourTab(). 在這4個函數中我們使用TabSpec來描述每個的Tab,並且設置Intent,完成點擊該Tab時跳轉到相應的Activity的功能。

當然,這個應用還有一個關鍵點,就是這裏的佈局tabs.xml.其代碼爲:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?> 
<TabHost 
    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"
      
        <TabWidget android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:paddingLeft="1dip" 
            android:paddingRight="1dip" 
            android:paddingTop="4dip" 
        /> 
           
        <FrameLayout android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" 
            android:layout_height="0dip" 
            android:layout_weight="1" 
        />      
    </LinearLayout
</TabHost>

注意在tabs.xml中,定義TabHost標籤,並且其中有一個TabWidget標籤是裝載整個Tabs的,其id必須爲android:id/tabs

完成這些工作之後,接下來的任務就是定義前面的幾個Activity,OneActivity,TwoActivity,ThreeActivity,FourActivity.這些Activity由大家根據自己的功能設定。

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