[英]Modify tab indicator dynamically in Android
My application needs to update tab indicator dynamically, I'm trying to do this by invoke TabSpec.setIndicator(), but it doesn't work. 我的应用程序需要动态更新选项卡指示器,我试图通过调用TabSpec.setIndicator()来执行此操作,但它不起作用。 Here is my code:
这是我的代码:
In onCreate method of TabActivity: 在tabActivity的onCreate方法中:
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" 现在我需要将tab指示符更改为另一个字符串,例如“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?
所以我想知道在将tab指针添加到tabhost之后如何更改它? Many thanks.
非常感谢。
Solution 解
Thanks to @CommonsWare, I make it by using TabWidget.getChildAt, here is my code for your convenience: 感谢@CommonsWare,我使用TabWidget.getChildAt制作它,这是我方便的代码:
TextView title = (TextView) tabHost.getTabWidget().getChildAt(tabId).findViewById(android.R.id.title)
title.setText("xyz");
I apologize for my earlier incorrect answer, now deleted. 我为之前的错误答案道歉,现已删除。
Try using getChildViewAt()
on TabWidget
. 尝试在
TabWidget
上使用getChildViewAt()
。
我使用getChildTabViewAt(tabId)
而不是childviewAt(id)
来管理多个选项卡。
尝试这个:
TextView title = (TextView) TabHost.getTabWidget().getChildTabViewAt(tabId).findViewById(android.R.id.title);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.