main.xml
<?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"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>
</TabHost>
tab1.xml
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是tab1" />
</LinearLayout>
tab2.xml
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是tab2" />
</LinearLayout>
TabHostActivity.java
package com.edu.tabhost;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class TabHostActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabInit();
}
public void tabInit(){
TabHost th = this.getTabHost();//实例化TabHost
//第一个标签
Intent i1 = new Intent();
i1.setClass(this, tab1activity.class);
TabHost.TabSpec ts1 = th.newTabSpec("tab1");//创建标签项
ts1.setIndicator("标签1");//设置指示器文本
ts1.setContent(i1);//填充布局
th.addTab(ts1);//填加到TabHost
//第二个标签
Intent i2 = new Intent();
i2.setClass(this, tab2activity.class);
TabHost.TabSpec ts2 = th.newTabSpec("tab2");//创建标签项
ts2.setIndicator("标签2");//设置指示器文本
ts2.setContent(i2);//填充布局
th.addTab(ts2);//填加到TabHost
//默认标签
th.setCurrentTab(0);
}
}
tab1activity.java
package com.edu.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class tab1activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.tab1);
super.onCreate(savedInstanceState);
}
}
tab2activity.java
package com.edu.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class tab2activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.tab2);
super.onCreate(savedInstanceState);
}
}