2010-03-30 103 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); 
    } 
}