2010-03-30 101 views
6

我不知道我做了什么,但一段时间后,我的TabWidget有白色的标签,看起来非常漂亮。我从来没有在我的项目中设置主题或背景/前景色。下次我编译它时,它又回到了灰色的标签页。我的应用程序使用默认的黑暗主题。即使我将应用程序主题设置为亮起,选项卡仍然是灰色的。显然这是改变标签颜色的其他内容。有人知道怎么做吗?TabWidget白色前景色?

+0

你也许在两个不同版本的平台测试?标签样式在2.0中更改。另外,如果你可以发布截图,采用'DDMS',这将非常有帮助。 – 2010-03-30 07:55:13

+0

啊,是的。它来自编译1.6。有没有办法为2.0+手动设置相同的颜色? – Monstieur 2010-03-31 04:05:29

+0

我有这个问题,并确定它是AndroidManifest.xml中的'targetSdkVersion'属性导致它改变了我。 – 2010-07-02 14:05:26

回答

15

我由于Android 1.6的灯光主题(选项卡指示灯文字为白色)中的错误而导致出现问题。我能覆盖默认的主题如下:

  1. 我创建了一个从默认的主题,继承了自定义主题:

styles.xml

<style name="MyTheme" parent="@android:style/Theme.Light"> 
    <item name="android:tabWidgetStyle">@style/LightTabWidget</item> 
</style> 

<style name="LightTabWidget" parent="@android:style/Widget.TabWidget"> 
    <!-- set textColor to red, so you can verify that it applied. --> 
    <item name="android:textColor">#f00</item> 
</style> 

然后我申请的是主题通过将android:theme="@style/MyTheme"添加到我的AndroidManifest.xml<application />元素中,将其添加到我的应用程序中。

+0

thanx史蒂夫,它帮助我,你让我的生活 – 2010-09-21 10:34:43

1
public void onCreate(Bundle savedInstanceState)

  `tabHost = getTabHost(); 
      tabHost.setOnTabChangedListener(this); 
    tabHost.setCurrentTab(0); 
    setTabColor();` 

比在听者:

公共无效onTabChanged(字符串tabId){ setTabColor();

最后的功能,设置前景色和背景太:

public void setTabColor() { 
    // set foreground color: 
    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { 
     RelativeLayout rl = (RelativeLayout) tabHost.getTabWidget().getChildAt(i); 
     ImageView imageView = (ImageView) rl.getChildAt(0);// change it if you want it 
     TextView textView = (TextView) rl.getChildAt(1);//   
     textView.setTextColor(Color.parseColor("#FFFFFF")); 
    } 

    // set background color: 
    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#010101")); // unselected 
    } 
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#121288")); // selected 
} 
0

在onCreated():

tabHost.setCurrentTab(0); 

// Set tabs text color to white: 
TabWidget tabWidget = tabHost.getTabWidget(); 
int whiteColor = getResources().getColor(R.color.white); 
int someOtherColor = getResources().getColor(R.color.someOtherColor); 
for(int i = 0; i < tabWidget.getChildCount(); i++){ 
    View tabWidgetChild = tabWidget.getChildAt(i); 
    if(tabWidgetChild instanceof TextView){ 
     ((TextView) tabWidgetChild).setTextColor(whiteColor); 
    } else if(tabWidgetChild instanceof Button){ 
     ((Button) tabWidgetChild).setTextColor(whiteColor); 
    } else if(tabWidgetChild instanceof ViewGroup){ 
     ViewGroup vg = (ViewGroup)tabWidgetChild; 
     for(int y = 0; y < vg.getChildCount(); y++){ 
      View vgChild = vg.getChildAt(y); 
      if(vgChild instanceof TextView){ 
       ((TextView) vgChild).setTextColor(whiteColor); 
      } 
     } 
     vg.setBackgroundColor(someOtherColor); 
    } 
}