简体   繁体   English

如何从TabHost中删除选项卡

[英]How to remove tab from TabHost

In a TabHost widget, I can create a new tab with its content (Intent) using TabHost.addTab(TabHost.TabSpec tabSpec) . TabHost小部件中,我可以使用TabHost.addTab(TabHost.TabSpec tabSpec)创建一个包含其内容(Intent)的新选项卡。

We can remove all tabs we created by calling clearAllTabs() , but I can't figure out how to remove the tab or just replace the content (Intent) inside the tab with new Intent. 我们可以通过调用clearAllTabs()来删除我们创建的所有选项卡,但我无法弄清楚如何删除选项卡或只是用新的Intent替换选项卡内的内容(Intent)。

so what I need something like removeTab(int index) 所以我需要像removeTab(int index)

更容易:

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

Actually, clearAllTabs does that : 实际上,clearAllTabs做到了:

public void clearAllTabs() {
  mTabWidget.removeAllViews();
  initTabHost();
  mTabContent.removeAllViews();
  mTabSpecs.clear();
  requestLayout();
  invalidate();
}

And the method removeAllViews comes from the class ViewGroup . 方法removeAllViews来自ViewGroup类。 Luckily, ViewGroup does have methods to remove only one view : 幸运的是, ViewGroup确实有只删除一个视图的方法:

  • removeView(View view)
  • removeViewAt(int index)
  • removeViewInLayout(View view)

Knowing that, I would recommend to subclass TabWidget and TabHost to add the behaviour you need. 知道了,我会推荐给继承TabWidgetTabHost添加你需要的行为。 Maybe there is an easier way but that's the only one I can think of. 也许有一种更简单的方法,但这是我能想到的唯一方法。 Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM