<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab3页面"
android:textSize="40dp" />
<TextView
android:id="@+id/tab4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab4页面"
android:textSize="40dp" />
<TextView
android:id="@+id/tab5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab5页面"
android:textSize="40dp" />
</FrameLayout>
</LinearLayout>
</TabHost>
tab.xml
package com.example.zbytexttwo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class TabActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
// 步骤1:获得TabHost的对象,并进行初始化setup()
TabHost tabs = (TabHost) findViewById(R.id.tabhost);
tabs.setup();
//步骤2:获得TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator增加页的标签
LayoutInflater.from(this).inflate(R.layout.activity_main_jm, tabs.getTabContentView());
tabs.addTab(tabs.newTabSpec("tag1").setIndicator("主界面").setContent(R.id.tab01));
LayoutInflater.from(this).inflate(R.layout.activity_jin_qi, tabs.getTabContentView());
tabs.addTab(tabs.newTabSpec("tag2").setIndicator("近期消费").setContent(R.id.tab02));
/*增加第三个Tab */
TabHost.TabSpec spec = tabs.newTabSpec("Tag3");
spec.setContent(R.id.tab3);//单击Tab要显示的内容
/* 显示Tab3内容*/
spec.setIndicator("添加账单");
tabs.addTab(spec);
/*增加第4个Tab */
spec = tabs.newTabSpec("Tag4");
spec.setContent(R.id.tab4);//单击Tab要显示的内容
/* 显示Tab4内容*/
spec.setIndicator("修改账单");
tabs.addTab(spec);
/*增加第5个Tab */
spec = tabs.newTabSpec("Tag5");
spec.setContent(R.id.tab5);//单击Tab要显示的内容
/* 显示Tab5内容*/
spec.setIndicator("预期消费");
tabs.addTab(spec);
/* 步骤3:可通过setCurrentTab(index)指定显示的页,从0开始计算*/
tabs.setCurrentTab(0);
}
}
TabActivity