TabWidget白色前景色是什么?

6

我不知道我做了什么,但有一段时间我的TabWidget的标签是白色的,看起来非常好。我从未在我的项目中设置过主题或背景/前景颜色。下一次编译它时,它就恢复为灰色标签了。我的应用程序正在使用默认的暗色主题。即使我将应用程序主题设置为亮色,标签仍然是灰色的。因此,显然是其他东西改变了标签的颜色。有人知道如何做到这一点吗?


你是否在两个不同版本的平台上进行测试?选项卡样式在2.0中已更改。此外,如果您可以发布一个使用DDMS拍摄的屏幕截图,那将非常有帮助。 - Roman Nurik
啊,是的。这是由于编译1.6版本导致的。有没有办法手动设置2.0+版本相同的颜色? - Monstieur
我遇到了这个问题,并确定是AndroidManifest.xml中的targetSdkVersion属性导致它对我进行更改。 - Steve Pomeroy
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" 添加到我的 AndroidManifest.xml 文件中的 <application /> 元素来将该主题应用于我的应用程序。


6

无论如何,这将使选项卡的背景变为黑色。 - Monstieur

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
}

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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接