Android 如何从 TabHost 中删除选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3299845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:35:31  来源:igfitidea点击:

How to remove tab from TabHost

androidandroid-widget

提问by Anwar Chandra

In a TabHostwidget, I can create a new tab with its content (Intent) using TabHost.addTab(TabHost.TabSpec tabSpec).

TabHost小部件中,我可以使用TabHost.addTab(TabHost.TabSpec tabSpec)创建一个包含其内容(意图)的新选项卡。

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)

采纳答案by Sephy

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. Luckily, ViewGroupdoes have methods to remove only one view :

方法 removeAllViews 来自类ViewGroup。幸运的是,ViewGroup确实有方法可以只删除一个视图:

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

Knowing that, I would recommend to subclass TabWidgetand TabHostto add the behaviour you need. Maybe there is an easier way but that's the only one I can think of. Good luck

知道这一点,我会建议子类化TabWidgetTabHost添加您需要的行为。也许有一种更简单的方法,但这是我唯一能想到的方法。祝你好运

回答by 2red13

Much Easier:

更容易:

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