![](http://hi.csdn.net/attachment/201109/16/0_13161639359oI8.gif)
如图为TabHost的运行结果:在一个Activity里面显示不同的标签里的内容。
代码如下:
AppMain.java
package com.lxy;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
public class AppMain extends TabActivity {
private TabHost myTabHost;
private Button bt01;
private Button bt02;
private Button bt03;
private Intent it01;
private Intent it02;
private Intent it03;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initWidget();
initIntent();
}
private void initWidget(){
bt01 = (Button)findViewById(R.id.tab_ib_service);
bt02 = (Button)findViewById(R.id.tab_ib_history);
bt03 = (Button)findViewById(R.id.tab_ib_eixt);
bt01.setOnClickListener(new ButtonListener());
bt02.setOnClickListener(new ButtonListener());
bt03.setOnClickListener(new ButtonListener());
it01 = new Intent(this,Test01.class);
it02 = new Intent(this,Test02.class);
it03 = new Intent(this,Test03.class);
}
class ButtonListener implements OnClickListener{
public void onClick(View v) {
if(v.getId() == R.id.tab_ib_service){
myTabHost.setCurrentTabByTag("t1");
} else if(v.getId() == R.id.tab_ib_history){
myTabHost.setCurrentTabByTag("t2");
} else if(v.getId() == R.id.tab_ib_eixt){
myTabHost.setCurrentTabByTag("t3");
}
}
}
private void initIntent(){
this.myTabHost = getTabHost();
TabHost localTabHost = this.myTabHost;
// newTabSpec() ---> Get a new TabHost.TabSpec associated with this tab host.
// setIndicator() ---> Specify a label and icon as the tab indicator.
// setContent() ---> Specify an intent to use to launch an activity as the tab content.
localTabHost.addTab(
myTabHost.newTabSpec("t1")
.setIndicator("LIU", getResources().getDrawable(R.drawable.icon))
.setContent(it01));
localTabHost.addTab(
myTabHost.newTabSpec("t2")
.setIndicator("ZHEN", getResources().getDrawable(R.drawable.icon))
.setContent(it02));
localTabHost.addTab(
myTabHost.newTabSpec("t3")
.setIndicator("WEI", getResources().getDrawable(R.drawable.icon))
.setContent(it03));
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<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="1.0dip"
android:layout_weight="1.0">
</FrameLayout>
<TabWidget android:id="@android:id/tabs" android:visibility="gone"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1.0" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="70px">
<Button android:id="@+id/tab_ib_service"
android:layout_height="70px" android:layout_width="fill_parent"
android:layout_weight="1" android:text="Tab1"
android:scaleType="fitXY" >
</Button>
<Button android:id="@+id/tab_ib_history"
android:layout_height="70px" android:layout_width="fill_parent"
android:layout_weight="1" android:text="Tab2"
android:scaleType="fitXY"></Button>
<Button android:id="@+id/tab_ib_eixt"
android:layout_height="70px" android:layout_width="fill_parent"
android:layout_weight="1" android:text="Tab3"
android:scaleType="fitXY">
</Button></LinearLayout>
</LinearLayout>
</TabHost>
Text01.java
package com.lxy;
import android.app.Activity;
import android.os.Bundle;
public class Test01 extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test01);
}
}
test01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000FF"
android:text="TEST01"></TextView>
</LinearLayout>
Text02.java, Text03.java与Text.01.java 基本一样,只是加载自己对应的布局文件
而布局文件text02.xml, text03.xml与text01.xml基本一样,只是文本中显示的内容不同,背景颜色不同(当然也可以相同)。