2010-10-11 71 views
0

我使用3個選項卡創建一個TabHost1:tab1,tab2,tab3。當選擇tab2時,我希望將TabHost2更改爲帶有2個選項卡的另一個子選項卡主題:tab4,tab5。如何在android中創建子標籤?

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"/> 
      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:padding="5dp" /> 
      </RelativeLayout> 
     </LinearLayout> 
</TabHost> 

活動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相同TabHost1.xml

當我選擇tab2時,像TabHost2級聯TabHost1

其實我想要TabHost2覆蓋(替換)TabHost1.How我們可以做到嗎?

回答

1

在「tab2」中放置第二個TabHost(和伴隨的TabWidgetFrameLayout,包含「tab4」和「tab5」)。

如果這與您正在尋找的內容不符,請編輯您的問題並提供更多詳細信息,如果可能,請附上照片。

+0

嗨,感謝您的回覆,我剛編輯我的問題。無法上傳圖片bcauze新角... – hdat 2010-10-12 02:45:50

相關問題