开发者

How to create a sub tabs in android?

开发者 https://www.devze.com 2023-01-19 07:34 出处:网络
I create a TabHost1 with 3 tabs: tab1, tab2, tab3. When choose tab2, I want the TabHost2 change into another sub TabHost with 2 tabs: tab4, tab5.

I create a TabHost1 with 3 tabs: tab1, tab2, tab3. When choose tab2, I want the TabHost2 change into another sub TabHost with 2 tabs: tab4, tab5.

TabHost1.java:

    Resources res = getResources();
    TabHost tabHost1 = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Tab1.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost1.newTabSpec("Tab1").setIndicator("Tab1",
                      res.getDrawable(R.drawable.home))
                  .setContent(intent);
    tabHost1.addTab(spec);

    intent = new Intent().setClass(this, Tab2.class);
    spec = tabHost1.newTabSpec("Tab2").setIndicator("Tab2",
            res.getDrawable(R.drawable.music))
        .setContent(intent);
    tabHost1.addTab(spec);

    intent = new Intent().setClass(this, Tab3.class);
    spec = tabHost1.newTabSpec("Tab3").setIndicator("Tab3",
            res.getDrawable(R.drawable.album))
        .setContent(intent);
    tabHost1.addTab(spec);
    tabHost1.setCurrentTab(0);

TabHost1.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="fill_parent"
   android:layout_height="fill_parent">
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
           <RelativeLayout 
           android:layout_width="fill_parent"
           android:layout_height="fill_parent">
           <TabWidget
              android:id="@android:id/tabs"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" 
              android:layout_alignParentBottom="true"/>
  开发者_如何学C         <FrameLayout
              android:id="@android:id/tabcontent"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="5dp" />
           </RelativeLayout>
      </LinearLayout>
</TabHost>

Activity Tab2.java:

    Resources res = getResources();
    TabHost tabHost2 = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, tab4.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost2.newTabSpec("tab4").setIndicator("tab4",
                      res.getDrawable(R.drawable.tab4))
                  .setContent(intent);
    tabHost2.addTab(spec);

    intent = new Intent().setClass(this, tab5.class);
    spec = tabHost2.newTabSpec("tab5").setIndicator("tab5",
            res.getDrawable(R.drawable.tab5))
        .setContent(intent);
    tabHost2.addTab(spec);
    tabHost2.setCurrentTab(0);

TabHost2.xml is the same TabHost1.xml

When I choose tab2 the result like TabHost2 cascaded TabHost1

Actually I want the TabHost2 overwrite (replace) TabHost1.How can we do it ?


Put a second TabHost (and accompanying TabWidget and FrameLayout, containing "tab4" and "tab5") in "tab2".

If that does not match what you are seeking, please edit your question with more details, including a picture if possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消