ActionBarActivity with TabHost

╄→尐↘猪︶ㄣ 提交于 2019-12-24 19:16:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!