Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

android - How to change the title of the Tab Dynamically

I have three tabs in my Application. On an event under one Tab, i want to change the title of an another existing Tab. This is the title that we provide while adding the tabs to the TabHost.

Eg: TabHost.addTab(tabHost.newTabSpec("Tab2")).setIndicator("I need to be Changed dynamically").setContent....

In the above example, the title of the tab2 that i provided under setIndicator(), should be changed dynamically.

Is there any way to accomplish this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unless you can find a cleaner method, you can access the TabWidget itself. Contained in the TabWidget are relative layouts for each of your tabs which each contain an ImageView and a TextView. To directly access the textview in the tab at index 0 you can do this:

mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);

Then just cast as a TextView and you can edit it however you want. The below worked for me:

((TextView)mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title)).setText("New");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...