如何在Android中向TabHost添加运行时选项卡?

如何在Android中向TabHost添加运行时选项卡?,android,layout,tabs,android-tabhost,Android,Layout,Tabs,Android Tabhost,你好,我正在开发一个应用程序 这需要在android中添加运行时选项卡,所有选项卡都应该动态添加 我的要求是: 我想添加带有5个选项卡的Tabhost,这些选项卡将显示屏幕 但有时它只需要3个标签在屏幕上,然后设计不应该改变 同样,如果我想添加超过5个标签,那么标签应该滚动运行时间,主要是设计不应该改变 谁能告诉我怎么做 目前我正在使用以下代码: public class Story_List extends ActivityGroup { ListVi

你好,我正在开发一个应用程序 这需要在android中添加运行时选项卡,所有选项卡都应该动态添加

我的要求是:

  • 我想添加带有5个选项卡的Tabhost,这些选项卡将显示屏幕
  • 但有时它只需要3个标签在屏幕上,然后设计不应该改变
  • 同样,如果我想添加超过5个标签,那么标签应该滚动运行时间,主要是设计不应该改变
谁能告诉我怎么做

目前我正在使用以下代码:

 public class Story_List extends ActivityGroup  
    {
            ListView list_stories;
            TabHost tab_stories;

            @SuppressWarnings("deprecation")
            @Override
            protected void onCreate(Bundle savedInstanceState) 
            {
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                super.onCreate(savedInstanceState);
                setContentView(R.layout.story_list);

                tab_stories=(TabHost)findViewById(R.id.tabhoststories);
                tab_stories.setup(this.getLocalActivityManager());

                setupTab1(new TextView(this), "Album 1");
                setupTab2(new TextView(this), "Album 2");
                setupTab3(new TextView(this), "Album 3");
            }

            private void setupTab1(final View view, final String tag) 
            {

                View tabview = createTabView(tab_stories.getContext(), tag);

                Intent intent = new Intent().setClass(this, StoryAlbum1.class);
                TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
                tab_stories.addTab(tab);
            }
            private void setupTab2(final View view, final String tag) 
            {

                View tabview = createTabView(tab_stories.getContext(), tag);

                Intent intent = new Intent().setClass(this, StoryAlbum2.class);
                TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
                tab_stories.addTab(tab);
            }
            private void setupTab3(final View view, final String tag) 
            {

                View tabview = createTabView(tab_stories.getContext(), tag);

                Intent intent = new Intent().setClass(this, StoryAlbum3.class);
                TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
                tab_stories.addTab(tab);
            }

            private static View createTabView(final Context context, final String text) 
            {
                View view = LayoutInflater.from(context).inflate(R.layout.tabs_text, null);
                TextView tv = (TextView) view.findViewById(R.id.tabsText);
                tv.setText(text);
                return view;
            }
}
试试这个:

TabHost.TabSpec tabSpec = tabHost.newTabSpec("Tab1");
    tabSpec.setContent(R.id.btnTab);
    tabSpec.setIndicator("My Tab 1");
    tabHost.addTab(tabSpec);
    btnAddTab.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TabHost.TabSpec spec = tabHost.newTabSpec("Tab"+i);
            spec.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return new AnalogClock(MainActivity.this);
                }
            });
            spec.setIndicator("Clock");
            tabHost.addTab(spec);
        }
    });

你能告诉我什么是AnalogClock吗?它只是android内置类在新创建的选项卡中设置视图,然后删除它。并返回任何视图(例如返回新的TextView(MainActivity.this));