2011-12-28 83 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")); 

     }