在Android中动态修改选项卡指示器

发布于 2024-09-03 03:01:04 字数 756 浏览 1 评论 0原文

我的应用程序需要动态更新选项卡指示器,我尝试通过调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青萝楚歌 2024-09-10 03:01:04

我为之前的错误答案道歉,现已删除。

尝试使用 getChildViewAt()TabWidget 上的 a>。

I apologize for my earlier incorrect answer, now deleted.

Try using getChildViewAt() on TabWidget.

寻梦旅人 2024-09-10 03:01:04

我使用 getChildTabViewAt(tabId) 而不是 childviewAt(id) 来处理多个选项卡。

i managaed with getChildTabViewAt(tabId) instead of childviewAt(id) for multiple tab.

青柠芒果 2024-09-10 03:01:04

试试这个:

 TextView title = (TextView) TabHost.getTabWidget().getChildTabViewAt(tabId).findViewById(android.R.id.title);

Try this:

 TextView title = (TextView) TabHost.getTabWidget().getChildTabViewAt(tabId).findViewById(android.R.id.title);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文