尝试使用Fragements
inside TabHost
,如下所示:
<TabHost
android:id="@android: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"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
然后通过执行以下操作初始化您的选项卡:
/**
* Initialise the Tab Host
*/
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
RedeemActivity.AddTab(this, this.mTabHost,
this.mTabHost.newTabSpec("Tab1").setIndicator("1"),
(tabInfo = new TabInfo("Tab1", Activity1.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
RedeemActivity.AddTab(this, this.mTabHost,
this.mTabHost.newTabSpec("Tab2").setIndicator("2"),
(tabInfo = new TabInfo("Tab2", Activity2.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
RedeemActivity.AddTab(this, this.mTabHost,
this.mTabHost.newTabSpec("Tab3").setIndicator("3"),
(tabInfo = new TabInfo("Tab3", Activity3.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
// Default to first tab
// this.onTabChanged("Tab1");
//
mTabHost.setOnTabChangedListener(this);
}