TabHost Tab的添加和删除

TabHost 添加Tab项:

[java]  view plain copy
  1. tabhost = this.getTabHost();  
  2.   
  3.         TabSpec tabSpec = tabhost.newTabSpec("news");  
  4.         tabSpec.setIndicator("新闻");  
  5.         tabSpec.setContent(new Intent(this, NewsActivity.class));  
  6.         tabhost.addTab(tabSpec);  
  7.   
  8.         TabSpec tabSpec2 = tabhost.newTabSpec("fun");  
  9.         tabSpec2.setIndicator("娱乐");  
  10.         tabSpec2.setContent(new Intent(this,FunActivity.class));  
  11.         tabhost.addTab(tabSpec2);  
  12.   
  13.         TabSpec tabSpec3 = tabhost.newTabSpec("sport");  
  14.         tabSpec3.setIndicator("体育");  
  15.         tabSpec3.setContent(new Intent(this,SportsActivity.class));  
  16.         tabhost.addTab(tabSpec3);  
  17.   
  18.         TabSpec tabSpec4 = tabhost.newTabSpec("setting");  
  19.         tabSpec4.setIndicator("设置");  
  20.         tabSpec4.setContent(new Intent(this, SettingActivity.class));  
  21.         tabhost.addTab(tabSpec4);  

TabHost删除Tab项

[java]  view plain copy
  1. mTabHost.getTabWidget().removeViewAt(mTabHost.getCurrentTab());  

这样删除会有问题。

只能通过删除所有Tab项然后再依次添加。需要注意在调用clearAllTabs()方法之前,需要设置当前显示的tab,即setCurrentTab(0),否则出现空指针问题。

完整代码如下:

[java]  view plain copy
  1. tabhost.setCurrentTab(0);  
  2.                 tabhost.clearAllTabs();  
  3.               
  4.                 TabSpec tabSpec = tabhost.newTabSpec("news");  
  5.                 tabSpec.setIndicator("新闻");  
  6.                 tabSpec.setContent(new Intent(this,  
  7.                         NewsActivity.class));  
  8.                 tabhost.addTab(tabSpec);  
  9.   
  10.                 TabSpec tabSpec2 = tabhost.newTabSpec("fun");  
  11.                 tabSpec2.setIndicator("娱乐");  
  12.                 tabSpec2.setContent(new Intent(this,  
  13.                         FunActivity.class));  
  14.                 tabhost.addTab(tabSpec2);  
  15.   
  16.                 TabSpec tabSpec3 = tabhost.newTabSpec("sport");  
  17.                 tabSpec3.setIndicator("体育");  
  18.                 tabSpec3.setContent(new Intent(this,  
  19.                         SportActivity.class));  
  20.                 tabhost.addTab(tabSpec3);  
  21.   
  22.                 TabSpec tabSpec4 = tabhost.newTabSpec("setting");  
  23.                 tabSpec4.setIndicator("设置");  
  24.                 tabSpec4.setContent(new Intent(EarthActivity.this,  
  25.                         SettingActivity.class));  
  26.                 tabhost.addTab(tabSpec4);  
  27.   
  28.                 tabhost.setCurrentTab(2);  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值