Android中的动态标签,带有动态标签内容

时间:2010-12-04 10:45:32

标签: android listview tabs

我想在Android中动态创建一个标签,在每个标签下我都有一个列表视图。所以我希望listview的内容也动态变化。我怎么能这样做?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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);  
}

你可以这样做。