0

例如,请参阅以下教程,了解我的选项卡最初是如何设置的。

我想要另一个 TabActivity,而不是在其中一个选项卡上运行正常活动。所以我想要做的是在 TabActivity 中运行 TabActivity。我认为问题在于ID的冲突。我试图通过更改辅助活动的 xml 文件上的 ID 并在活动中手动调用这些 ID 来解决此问题,但没有运气。

我一直在寻找解决这个问题的几个小时,但一无所获。

4

1 回答 1

0

一个应用程序的多个选项卡活动是可能的。

例如:应用程序包含一个带有两个选项卡的 Launcher TabActivity (HomeTabActivity):选项卡 1 和选项卡 2

Tab 1 can be a TabActivity with two or more tabs.
Tab 2 can be a TabActivity with two or more tabs. 

FirstTab, SecondTab, ThirdTab and FourthTab are simple activities actings as child for child of HomeTabActivity.

xml files containing TabHost as parent element
1. hometab.xml
2. tab1.xml
3. tab2.xml

To differentiate between HomeTabActivity and Its child TabActivities i.e Tab1 and Tab2 
I have put TabWidget at top for HomeTabActivity and at bottom for Tab1 and Tab2.

HomeTabActivity(第一个主选项卡活动):

public class HomeTabActivity extends TabActivity 
{
    private TabHost mTabHost = null;
    private Intent mIntent = null;
    private TabHost.TabSpec mTabSpec = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hometab); 
        initializeTabs();
    }

    private void initializeTabs() {
        mTabHost = getTabHost();

        mIntent = new Intent().setClass(this, Tab1.class);
        mTabSpec = mTabHost
                .newTabSpec("Tab1")
                .setIndicator("Tab1",
                        getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(mIntent);
        mTabHost.addTab(mTabSpec);

        mIntent = new Intent().setClass(this, Tab2.class);
        mTabSpec = mTabHost
                .newTabSpec("Tab2")
                .setIndicator("Tab2",
                        getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(mIntent);
        mTabHost.addTab(mTabSpec);

        mTabHost.setCurrentTab(0);
    }

}

Tab1(嵌入在 HomeTabActivity 中的 TabActivity):

public class Tab1 extends TabActivity 
{
    private TabHost mTabHost = null;
    private Intent mIntent = null;
    private TabHost.TabSpec mTabSpec = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);
        initializeTabs();
    }

    private void initializeTabs() {
        mTabHost = getTabHost();
        mIntent = new Intent().setClass(this, FirstTab.class);

        mTabSpec = mTabHost
                .newTabSpec("Tab1 Child 1")
                .setIndicator("Tab1 Child 1",
                        getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(mIntent);
        mTabHost.addTab(mTabSpec);

        mIntent = new Intent().setClass(this, SecondTab.class);
        mTabSpec = mTabHost
                .newTabSpec("Tab1 Child 2")
                .setIndicator("Tab1 Child 2",
                        getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(mIntent);
        mTabHost.addTab(mTabSpec);
        mTabHost.setCurrentTab(0);
    }
}

Tab2(嵌入在 HomeTabActivity 中的另一个 TabActivity):

public class Tab2 extends TabActivity 
{
    private TabHost mTabHost = null;
    private Intent mIntent = null;
    private TabHost.TabSpec mTabSpec = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab2);
        initializeTabs();
    }

    private void initializeTabs() {
        mTabHost = getTabHost();
        mIntent = new Intent().setClass(this, ThirdTab.class);

        mTabSpec = mTabHost
                .newTabSpec("Tab2 Child 1")
                .setIndicator("Tab2 Child 1",
                        getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(mIntent);
        mTabHost.addTab(mTabSpec);

        mIntent = new Intent().setClass(this, FourthTab.class);
        mTabSpec = mTabHost
                .newTabSpec("Tab2 Child 2")
                .setIndicator("Tab2 Child 2",
                        getResources().getDrawable(R.drawable.ic_launcher))
                .setContent(mIntent);
        mTabHost.addTab(mTabSpec);
        mTabHost.setCurrentTab(1);
    }
}

主页选项卡.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

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

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

</TabHost>

tab1.xml 和 tab2.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:background="@color/androidblue"
            android:layout_weight="1">
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="0">
        </TabWidget>
    </LinearLayout>

</TabHost>
于 2013-02-07T11:31:34.593 回答