转自: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
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"
>
<
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由大家根据自己的功能设定。