簡體   English   中英

一個應用程序中有多個Tab活動

[英]Multiple Tab activities in one application

作為示例,請參閱以下教程 ,了解如何最初設置我的標簽。

我希望在另一個選項卡上運行正常的活動,而不是在其中一個選項卡上運行。 所以我想做的是在TabActivity中運行TabActivity。 我認為問題在於ID的沖突。 我試圖通過更改輔助活動的xml文件上的ID並在活動中手動調用這些ID來解決此問題,但是沒有運氣。

我一直在尋找解決方案,但是卻一無所獲。

一個應用程序可以進行多個選項卡活動。

例如:應用程序包含帶有兩個選項卡的啟動器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(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(另一個嵌入在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

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM