2011-12-28 81 views
2

我試圖更改TabActivity中選項卡的背景顏色。 因爲我不喜歡以下,在TabActivity中更改選項卡背景顏色

tabHost.getTabWidget().getChildAt(totalTabs1-1).setBackgroundColor(Color.parseColor("#984b9d")); 

但它不能正常工作,我想要的。

有沒有其他方法可以做到這一點?

謝謝

回答

3

對於這個u必須寫繪製文件夾選項卡選擇一個XML文件。

tab_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item android:drawable="@drawable/tab_selectinfo" 
      android:state_selected="true" /> 
    <!-- When not selected, use white--> 
    <item android:drawable="@drawable/tab_unselectinfo" /> 
</selector> 

,並在選項卡的初始化時間只是做類似下面,

tabHost.newTabSpec("Info").setIndicator("Info", res.getDrawable(R.drawable.tab_selector)).setContent(intent); 
1

你可以試試這個:

... 
setTabColor(tabHost); 
tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

    @Override 
    public void onTabChanged(String arg0) { 

     setTabColor(tabHost); 
    } 
});  
... 
//Change The Backgournd Color of Tabs 
public void setTabColor(TabHost tabhost) {  

    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
      tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.DKGRAY); //unselecte 
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.LTGRAY); // selected 
} 
0

使用以下

for (int i = 0; i < Global.host.getTabWidget().getChildCount(); i++) { 
      Global.host.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.inactbg)); 
      TextView tv = (TextView) Global.host.getTabWidget().getChildAt(i).findViewById(android.R.id.title); 
      tv.setTextColor(Color.parseColor("#ffffff")); 

     } 
相關問題