6

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

4

4 回答 4

14

由于 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"到 my 的<application />元素将该主题应用到我的应用程序中AndroidManifest.xml

于 2010-07-02T14:33:40.840 回答
6

检查我的这个答案:标签小部件中的背景忽略缩放

也可以参考android.graphics.drawable

在您的代码中,您可以像这样设置标签的背景:

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(
            android.R.color.white);
于 2010-03-30T09:00:03.373 回答
1

在里面public void onCreate(Bundle savedInstanceState)

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

比听众:

public void onTabChanged(String 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
}
于 2011-07-20T11:34:32.920 回答
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);
    }
}
于 2013-11-01T13:30:43.653 回答