2014-02-24 34 views
0

你好,我正在開發一個應用程序 這需要在android中添加運行時間選項卡並且所有選項卡應該動態添加。如何將運行時間選項卡添加到Android中的TabHost?

我的要求是:

  • 我想用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; 
      } 
} 

回答

0

試試這個:

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); 
     } 
    }); 
+0

你能告訴我什麼是AnalogClock? –

+0

它只是在構建在新創建的選項卡中設置視圖的類中。 – Vijju

+0

ohk但它有錯誤。 –

相關問題