2012-05-18 34 views
-1
TabWidget tw = getTabWidget(); 

    for (int i = 0; i < tw.getChildCount(); i++) { 
     View v = tw.getChildAt(i); 


     v.setBackgroundColor(Color.parseColor("#666666")); 
    } 
    tw.getChildAt(0).setBackgroundColor(Color.parseColor("#B21206")); 

我的活動中有兩個選項卡。此代碼將第一個選項卡的顏色固定爲「紅色」,但不會使第二個選項卡在單擊時變爲「紅色」。 應該在代碼中做什麼更改?如何在點擊時更改標籤的顏色?

+0

檢查我的編輯,並嘗試 – Akram

回答

2

試試這個代碼在你tabactivity類..它可以幫助你......

for(int i = 0; i < getTabWidget().getChildCount(); i++) 
    { 
     if(getTabWidget().getChildAt(i).isSelected()) 
     { 
      //selected tab 
      getTabWidget().getChildAt(i).setBackgroundColor(Color.RED); 
     } 
     else 
     { 
      //un-selected tabs 
      getTabWidget().getChildAt(i).setBackgroundColor(Color.GREEN); 
     } 
    } 
1

爲你的任務使用xml bg選擇器,這是做你想做的更好的方法。

如果你想在代碼的

試試這個代碼

通過你的標籤主機settabColor(yourtabhost);

public static void setTabColor(TabHost tabhost) { 
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
    { 
     tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected 
    } 
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected 
} 
+0

但將不是爲整個頁面的背景是什麼? – Shiven

+0

您想要更改活動背景或選項卡背景的第一件事。 – Akram

+0

選項卡背景。 – Shiven

相關問題