在Android中动态修改选项卡指示器
我的应用程序需要动态更新选项卡指示器,我尝试通过调用 TabSpec.setIndicator() 来完成此操作,但它不起作用。这是我的代码:
在 TabActivity 的 onCreate 方法中:
tabHost = getTabHost();
TabSpec tabSpec = tabHost.newTabSpec("abc");
tabSpec.setIndicator("helloabc");
tabSpec.setContent(new MyViewFactory());
tabHost.addTab(tabSpec);
现在我需要将选项卡指示器更改为另一个字符串,例如“xyz”,
TabSpec tabSpec = MyTabActivity.getTabSpec();
tabSpec.setIndicator("xyz");
但它不起作用。所以我想知道如何在将选项卡指示器添加到选项卡主机后更改选项卡指示器?非常感谢。
解决方案
感谢@CommonsWare,我通过使用TabWidget.getChildAt来实现,为了您的方便,这里是我的代码:
TextView title = (TextView) tabHost.getTabWidget().getChildAt(tabId).findViewById(android.R.id.title)
title.setText("xyz");
My application needs to update tab indicator dynamically, I'm trying to do this by invoke TabSpec.setIndicator(), but it doesn't work. Here is my code:
In onCreate method of TabActivity:
tabHost = getTabHost();
TabSpec tabSpec = tabHost.newTabSpec("abc");
tabSpec.setIndicator("helloabc");
tabSpec.setContent(new MyViewFactory());
tabHost.addTab(tabSpec);
Now I need to change tab indicator to another string, for example, "xyz"
TabSpec tabSpec = MyTabActivity.getTabSpec();
tabSpec.setIndicator("xyz");
But it doesn't work. So I'd like to know how to change tab indicator after it is added to the tabhost? Many thanks.
Solution
Thanks to @CommonsWare, I make it by using TabWidget.getChildAt, here is my code for your convenience:
TextView title = (TextView) tabHost.getTabWidget().getChildAt(tabId).findViewById(android.R.id.title)
title.setText("xyz");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我为之前的错误答案道歉,现已删除。
尝试使用
getChildViewAt()
TabWidget 上的 a>。I apologize for my earlier incorrect answer, now deleted.
Try using
getChildViewAt()
onTabWidget
.我使用
getChildTabViewAt(tabId)
而不是childviewAt(id)
来处理多个选项卡。i managaed with
getChildTabViewAt(tabId)
instead ofchildviewAt(id)
for multiple tab.试试这个:
Try this: