第一步配置布局文件
01 |
< TabHost |
02 |
android1:id = "@android:id/tabhost" |
03 |
android1:layout_width = "match_parent" |
04 |
android1:layout_height = "match_parent" |
05 |
android1:layout_weight = "1" > |
06 |
07 |
< LinearLayout |
08 |
android1:layout_width = "match_parent" |
09 |
android1:layout_height = "match_parent" |
10 |
android1:orientation = "vertical" > |
11 |
12 |
< TabWidget |
13 |
android1:id = "@android:id/tabs" |
14 |
android1:layout_width = "match_parent" |
15 |
android1:layout_height = "wrap_content" > |
16 |
</ TabWidget > |
17 |
18 |
< FrameLayout |
19 |
android1:id = "@android:id/tabcontent" |
20 |
android1:layout_width = "match_parent" |
21 |
android1:layout_height = "match_parent" > |
22 |
23 |
< LinearLayout |
24 |
android1:id = "@+id/tab1" |
25 |
android1:layout_width = "match_parent" |
26 |
android1:layout_height = "match_parent" > |
27 |
28 |
< ListView |
29 |
android1:id = "@+id/listview1" |
30 |
android1:layout_width = "fill_parent" |
31 |
android1:layout_height = "fill_parent" > |
32 |
</ ListView > |
33 |
</ LinearLayout > |
34 |
35 |
< LinearLayout |
36 |
android1:id = "@+id/tab2" |
37 |
android1:layout_width = "match_parent" |
38 |
android1:layout_height = "match_parent" > |
39 |
</ LinearLayout > |
40 |
</ FrameLayout > |
41 |
</ LinearLayout > |
42 |
</ TabHost > |
1 |
< LinearLayout |
2 |
android1:id = "@+id/tab2" |
3 |
android1:layout_width = "match_parent" |
4 |
android1:layout_height = "match_parent" > |
5 |
</ LinearLayout > |
在linearlayout中设置你需要的控件,我这里放了一个listview
第二步 配置activity
1 |
final TabHost
tabhost = (TabHost) findViewById(android.R.id.tabhost); |
2 |
tabhost.setup(); |
3 |
TabWidget
tabwidget = tabhost.getTabWidget(); |
4 |
tabhost.addTab(tabhost.newTabSpec( "tab1" ).setIndicator( "tab1" ) |
5 |
.setContent(R.id.tab1)); |
6 |
tabhost.addTab(tabhost.newTabSpec( "tab2" ).setIndicator( "tab2" ) |
7 |
.setContent(R.id.tab2)); |
第三步 事件监听
01 |
tabhost.setOnTabChangedListener( new OnTabChangeListener()
{ |
02 |
public void onTabChanged(String
arg0) { |
03 |
//
TODO Auto-generated method stub |
04 |
Toast.makeText(DiquActivity. this , "当前标签页为" +
arg0, |
05 |
Toast.LENGTH_SHORT).show(); |
06 |
} |
07 |
}); |
08 |
09 |
tabhost.getTabWidget().getChildAt( 0 ) |
10 |
.setOnClickListener( new OnClickListener()
{ |
11 |
12 |
@Override |
13 |
public void onClick(View
v) { |
14 |
//
TODO Auto-generated method stub |
15 |
tabhost.setCurrentTab( 0 ); |
16 |
Toast.makeText(DiquActivity. this , "当前标签页为1" , |
17 |
Toast.LENGTH_SHORT).show(); |
18 |
} |
19 |
}); |
20 |
21 |
tabhost.getTabWidget().getChildAt( 1 ) |
22 |
.setOnClickListener( new OnClickListener()
{ |
23 |
24 |
@Override |
25 |
public void onClick(View
v) { |
26 |
//
TODO Auto-generated method stub |
27 |
tabhost.setCurrentTab( 1 ); |
28 |
Toast.makeText(DiquActivity. this , "当前标签页为2" , |
29 |
Toast.LENGTH_SHORT).show(); |
30 |
} |
31 |
}); |