Modify tab indicator dynamically in Android

2020-06-11 11:03发布

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");

3条回答
贪生不怕死
2楼-- · 2020-06-11 11:17

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

查看更多
够拽才男人
3楼-- · 2020-06-11 11:30

Try this:

 TextView title = (TextView) TabHost.getTabWidget().getChildTabViewAt(tabId).findViewById(android.R.id.title);
查看更多
走好不送
4楼-- · 2020-06-11 11:41

I apologize for my earlier incorrect answer, now deleted.

Try using getChildViewAt() on TabWidget.

查看更多
登录 后发表回答