TabHost Tab的添加和删除

TabHost 添加Tab项:

tabhost = this.getTabHost();

		TabSpec tabSpec = tabhost.newTabSpec("news");
		tabSpec.setIndicator("新闻");
		tabSpec.setContent(new Intent(this, NewsActivity.class));
		tabhost.addTab(tabSpec);

		TabSpec tabSpec2 = tabhost.newTabSpec("fun");
		tabSpec2.setIndicator("娱乐");
		tabSpec2.setContent(new Intent(this,FunActivity.class));
		tabhost.addTab(tabSpec2);

		TabSpec tabSpec3 = tabhost.newTabSpec("sport");
		tabSpec3.setIndicator("体育");
		tabSpec3.setContent(new Intent(this,SportsActivity.class));
		tabhost.addTab(tabSpec3);

		TabSpec tabSpec4 = tabhost.newTabSpec("setting");
		tabSpec4.setIndicator("设置");
		tabSpec4.setContent(new Intent(this, SettingActivity.class));
		tabhost.addTab(tabSpec4);

TabHost删除Tab项

	mTabHost.getTabWidget().removeViewAt(mTabHost.getCurrentTab());

这样删除会有问题。

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

完整代码如下:

tabhost.setCurrentTab(0);
				tabhost.clearAllTabs();
			
				TabSpec tabSpec = tabhost.newTabSpec("news");
				tabSpec.setIndicator("新闻");
				tabSpec.setContent(new Intent(this,
						NewsActivity.class));
				tabhost.addTab(tabSpec);

				TabSpec tabSpec2 = tabhost.newTabSpec("fun");
				tabSpec2.setIndicator("娱乐");
				tabSpec2.setContent(new Intent(this,
						FunActivity.class));
				tabhost.addTab(tabSpec2);

				TabSpec tabSpec3 = tabhost.newTabSpec("sport");
				tabSpec3.setIndicator("体育");
				tabSpec3.setContent(new Intent(this,
						SportActivity.class));
				tabhost.addTab(tabSpec3);

				TabSpec tabSpec4 = tabhost.newTabSpec("setting");
				tabSpec4.setIndicator("设置");
				tabSpec4.setContent(new Intent(EarthActivity.this,
						SettingActivity.class));
				tabhost.addTab(tabSpec4);

				tabhost.setCurrentTab(2);



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值