开发者

Modify tab indicator dynamically in Android

开发者 https://www.devze.com 2023-01-01 06:32 出处:网络
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:

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开发者_C百科.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.


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


Try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消