嘗試使用Fragements
內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);
}
凡在你的代碼是你,包括'activity_calculator.xml' ? – ozbek
我推薦使用Fragment而不是Tab Widget:D –
@Jeff Lee。它適用於Android版本8+。大多數手機仍然是2.3,這是API 10. – Chloe