2010-12-04 188 views
1

嗨,我想在Android和每個標籤下動態創建選項卡我有一個列表視圖。所以我希望listview的內容也動態地改變。我怎樣才能做到這一點 ?動態標籤在Android與動態標籤內容

在此先感謝。任何幫助將不勝感激。

回答

7

我在另一個論壇上看到了一個代碼,並將它清理了一下。要做到你需要什麼,你就必須改變ts1.setContent和ts2.setContent裏面的東西

Main_screen.java

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 

public class Main_screen extends Activity{ 

    private ListView ls1; 
    private ListView ls2; 
    private TabHost myTabHost; 

    @Override 
    public void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 

     myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost); 
     myTabHost.setup(); 

     ls1 = new ListView(Main_screen.this);  
     TabSpec ts1 = myTabHost.newTabSpec("TAB_TAG_1"); 
     ts1.setIndicator("Tab1"); 
     ts1.setContent(new TabHost.TabContentFactory(){ 
      public View createTabContent(String tag) 
      {         
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item1","item2","item3"}); 
       ls1.setAdapter(adapter);  
       return ls1; 
      }  
     }); 
     myTabHost.addTab(ts1); 

     ls2 = new ListView(Main_screen.this); 
     TabSpec ts2 = myTabHost.newTabSpec("TAB_TAG_2"); 
     ts2.setIndicator("Tab2"); 
     ts2.setContent(new TabHost.TabContentFactory(){ 
      public View createTabContent(String tag) 
      {         
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item4","item5","item6"}); 
       ls2.setAdapter(adapter);  
       return ls2; 
      }  
     });  
     myTabHost.addTab(ts2); 
    }  
} 

main.xml中

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

    <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="64dip" /> 

    <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:paddingTop="75dip"> 

      <ListView 
       android:id = "@+id/danhsach" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 

    </FrameLayout> 
</TabHost> 
0
for(int i=0; i<5; i++) 
{ 
    final TabSpec x=tabHost2.newTabSpec("x"); 
    View row = inflater.inflate(R.layout.indicator1,null); 
    final TextView indicator1 =(TextView) row.findViewById(R.id.textView_indicator1); 
    indicator1.setText(indicator_list[i]); 
    // indicator1.setShadowLayer(1, 0, 1, 0xFF013201); 

    x.setIndicator(row); 

    x.setContent(new TabContentFactory() { 
     public View createTabContent(String arg) { 
     return gallery2; 
     } 
    }); 

    tabHost2.addTab(x); 
} 

你可以這樣做。

+4

這個答案的解釋,以及它爲什麼可能是一個答案,對於那些後來發現這些答案的人來說是有用的。 – 2012-09-28 07:43:29