问题
I am developing an app, with minimum sdk level 7 and that's why I use support library and ActionBarActivity to have a proper ActionBar, but also the layout of my page contains four tabs on the bottom of the activity, and as i already extend ActionBarActivity, I cannot extend TabActivity. The problem is when I write
public class HomePageActivity extends ActionBarActivity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_homepage);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab1 = mTabHost.newTabSpec("TAB1");
tab1.setIndicator("TAB 1");
Intent popularContentIntent = new Intent().setClass(this, GuideActivity.class);
tab1.setContent(popularContentIntent);
mTabHost.addTab(tab1);
I get the error "Did you forget to call 'public void setup(LocalActivityManager activityGroup)?'" at the line when I trying to add tab to the tabhost. If I try to set the id of the view in the tab.setContent(..) like tab.setContent(R.id.tab1) and have a view in the xml with the id tab1, I get the error that the view with such a number cannot be found.
My layout:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs"
android:layout_alignParentTop="true" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</Button>
</TabWidget>
</RelativeLayout>
Is it possible somehow to have layout with tabs and actionbar at the same time? What I do wrong? What could be other alternatives? Thanks a lot!
回答1:
TabActivity
was deprecated in API level 13. The updated documentation recommends using android.support.v4.app.FragmentTabHost
instead.
An even better method would be to use the ViewPager
sample provided here.
来源:https://stackoverflow.com/questions/18703962/actionbaractivity-with-tabhost