我必须在所有标签中添加n个具有相同布局的标签(例如llItemList,它在xml中声明)。在下面的代码段中,当我创建n个标签时。所有布局都使用llItemList的相同副本,而不是选项卡自己的副本。您能否让我知道如何在将动态创建的所有选项卡中创建llItemList的单个副本。
提前致谢

    //---------------------------------
    // insert the tabs dynamically
    //---------------------------------
    for (int i=1; i<=n; i++){
        final String tabName = "tab" + Integer.toString(i);
        final TabSpec ourSpec = th.newTabSpec(tabName);
        ourSpec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
            ourSpec.setContent(R.id.llItemList);
            TextView text = new TextView(NewTicket.this);
            String tabText = tabName;
            text.setText("You've created a new tab " + tabText);
            return (text);
            }
        });
        ourSpec.setIndicator(tabName);
        th.addTab(ourSpec);
    }

最佳答案

您应该通过指定TabSpec


视图的ID,或者
创建视图内容的TabHost.TabContentFactory


您的代码兼有!

将您的代码更改为以下之一:

for (int i=1; i<=n; i++){
    final String tabName = "tab" + Integer.toString(i);
    final TabSpec ourSpec = th.newTabSpec(tabName);
    ourSpec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            TextView text = new TextView(NewTicket.this);
            String tabText = tabName;
            text.setText("You've created a new tab " + tabText);
            return (text);
        }
    });
    ourSpec.setIndicator(tabName);
    th.addTab(ourSpec);
}


要么

for (int i=1; i<=n; i++){
    final String tabName = "tab" + Integer.toString(i);
    final TabSpec ourSpec = th.newTabSpec(tabName);
    ourSpec.setContent(R.id.llItemList);
    ourSpec.setIndicator(tabName);
    th.addTab(ourSpec);
}


编辑:

如注释中所述,要保留ListView的相同实例,请执行以下操作:


注册TabHost.OnTabChangeListener
当选项卡更改时,您应该首先在ListView的当前父级上调用removeView(),然后将其addView()调用到新的父级。

10-07 19:23
查看更多