2014-01-26 51 views
0

我已經搜索了這個解決方案,但可以找到任何有用的東西,任何人都可以告訴我,我可以從TabHost中刪除特定的標籤/ TabSpec。我爲每個tabSpec製作對象並將其保存在arrayList中。下面的代碼:在Android中動態刪除標籤

public class Main extends Activity { 
    @Override 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    final TabHost tabs=(TabHost)findViewById(R.id.tabhost); 
    tabs.setup(); 
    final ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>(); 
      TabHost.TabSpec spec= tabs.newTabSpec("buttontab"); 
    spec.setContent(R.id.buttontab); 
    spec.setIndicator("Button"); 
    tabs.addTab(spec); 
    list.add(spec); 
    tabs.setCurrentTab(0); 

    Button btn=(Button)tabs.getCurrentView().findViewById(R.id.buttontab); 
      btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
      TabHost.TabSpec spec=tabs.newTabSpec("tag1"); 
      spec.setContent(new TabHost.TabContentFactory() { 
     public View createTabContent(String tag) { 
      return(new AnalogClock(Main.this)); 
     } 
    }); 
    spec.setIndicator("Clock"); 
    tabs.addTab(spec); 
    list.add(spec); 
    } 
}); 
    } 
} 

回答

0

您可以使用此代碼來實現這一

tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(3)); 

,並獲得了標籤編號,使用tabHost.getCurrentTab()

+0

有益的,但你也告訴我如何動態弄清楚我目前在哪個標籤?這將完全解決我的問題 –

+0

我已經告訴過你的問題,使用tabHost.getCurrentTab()..你可以在logcat或toastmessage中使用它來知道。如果它有幫助,請評價它 –