簡體   English   中英

如何在Android的TabHost中添加運行時選項卡?

[英]How to add Run Time Tabs into TabHost in Android?

您好,我正在開發一個需要在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);
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM