第一步:创建布局文件
<?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"
android:padding="5dp">
<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"
android:padding="5dp" />
</LinearLayout>
</TabHost>
第二步:创建三个Activity作为Tabhost内容
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is one tab");
setContentView(textview);
}
}
第三步:测试类
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is one tab");
setContentView(textview);
}
}
public class TabHostTestActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost=getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(new Intent(TabHostTestActivity.this
,Activity1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(new Intent(TabHostTestActivity.this
,Activity2.class)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(new Intent(TabHostTestActivity.this
,Activity3.class)));
tabHost.setCurrentTab(1);
}
}、
第四步:演示效果