2010-05-29 45 views
13

我的應用程序需要動態更新選項卡指示器,我試圖通過調用TabSpec.setIndicator()來做到這一點,但它不起作用。這裏是我的代碼:在Android中動態修改選項卡指示器

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

但它不」工作。所以我想知道如何更改選項卡指示器後,它被添加到tabhost?非常感謝。

解決方案

感謝@CommonsWare,我用TabWidget.getChildAt做到,這是我爲您提供方便代碼:

TextView title = (TextView) tabHost.getTabWidget().getChildAt(tabId).findViewById(android.R.id.title) 
title.setText("xyz"); 
+0

我可以改變圖像的指示這樣? – keen 2014-02-26 06:52:22

回答

13

我爲我剛纔的不正確的答案道歉,現在刪。

嘗試使用getChildViewAt() on TabWidget

+0

我如何更改標籤欄的圖像指示器? – keen 2014-02-26 06:52:50

7

我與getChildTabViewAt(tabId) manjied而不是childviewAt(id)爲多個選項卡。

1

試試這個:

TextView title = (TextView) TabHost.getTabWidget().getChildTabViewAt(tabId).findViewById(android.R.id.title); 
相關問題