使用TabHost布局不继承TabActivity

时间:2022-01-10 16:02:15

当继承Activity时

mTabHost = (TabHost) findViewById(R.id.tabhost);

LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, true);
mLocalActivityManager.dispatchCreate(savedInstanceState);

mTabHost.setup(mLocalActivityManager);


当继承ActivityGroup时

mTabHost = (TabHost) findViewById(R.id.tabhost);

mTabHost.setup();


注意的是:

TabHost、TabWidget和FrameLayout的id命名必须和系统的一致

<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" >
                </TabWidget>

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >

                    <LinearLayout
                        android:id="@+id/tab1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" >
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab2"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" >
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab3"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" >
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
        </TabHost>