简体   繁体   English

一个应用程序中有多个Tab活动

[英]Multiple Tab activities in one application

As an example, see the following tutorial on how my tabs are setup initially. 作为示例,请参阅以下教程 ,了解如何最初设置我的标签。

Instead of a normal activity running on one of the tabs, I want another TabActivity. 我希望在另一个选项卡上运行正常的活动,而不是在其中一个选项卡上运行。 So what I'm trying to do is run a TabActivity within a TabActivity. 所以我想做的是在TabActivity中运行TabActivity。 I believe the issue is that the ID's conflict. 我认为问题在于ID的冲突。 I have tried to solve this by changing the ID's on the secondary activity's xml file and calling those manually in the activity, but have had no luck. 我试图通过更改辅助活动的xml文件上的ID并在活动中手动调用这些ID来解决此问题,但是没有运气。

I have been searching for hours for a solution for this, but have come up with nothing. 我一直在寻找解决方案,但是却一无所获。

Multiple tab activities with an application is possible. 一个应用程序可以进行多个选项卡活动。

For instance: App contains a Launcher TabActivity (HomeTabActivity) with two tabs : Tab 1 and Tab 2 例如:应用程序包含带有两个选项卡的启动器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 (Very First Main Tab Activity): 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 (TabActivity Embedded inside the HomeTabActivity) : Tab1(TabActivity嵌入在HomeTabActivity内部):

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 (Another TabActivity Embedded inside the HomeTabActivity) : 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);
    }
}

hometab.xml hometab.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 and tab2.xml 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM