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

有帮助吗?

解决方案

由于 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"<application /> 我的元素 AndroidManifest.xml.

其他提示

看看我的这个回答: 选项卡小部件中的背景忽略缩放

您还可以参考 android.graphics.drawable 包裹

在您的代码中,您可以像这样设置选项卡的背景:

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(
            android.R.color.white);

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
}

在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);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top