如何在不让活动扩展TabActivity的情况下添加制表符布局?

时间:2011-10-20 09:53:15

标签: android tabactivity

1 个答案:

答案 0 :(得分:4)

将至少一个标签内容放入其中

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost = (TabHost) findViewById(R.id.mytabhost);
    tabHost.setup();

    TabSpec tab1 = tabHost.newTabSpec("TAB_1");
    tab1.setIndicator("Tab 1");
    tab1.setContent(R.id.tab1);
    tabHost.addTab(tab1);

    //tab 2 etc...
    TabSpec tab2 = tabHost.newTabSpec("TAB_2");
    tab2.setIndicator("Tab 2");
    tab2.setContent(R.id.tab2);
    tabHost.addTab(tab2);
}

并在xml中:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mytabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <LinearLayout android:id="@+id/LinearLayout01"
        android:orientation="vertical" android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <TabWidget android:id="@android:id/tabs" 
            android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>

        <FrameLayout android:id="@android:id/tabcontent" 
            android:layout_height="fill_parent" android:layout_width="fill_parent">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab1">
                <!-- tab 1 content goes here -->
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab2">
                <!-- tab 2 content goes here -->
            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

</TabHost>