【问题标题】:TabWidget white foreground color?TabWidget 白色前景色?
【发布时间】:2011-02-02 08:30:21
【问题描述】:

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

【问题讨论】:

  • 您是否在两个不同版本的平台上进行测试?选项卡样式在 2.0 中发生了变化。另外,如果您可以发布一张使用DDMS 拍摄的屏幕截图,那将非常有帮助。
  • 啊,是的。它来自为 1.6 编译。有什么方法可以手动为 2.0+ 设置相同的颜色?
  • 我遇到了这个问题,并确定是 AndroidManifest.xml 中的 targetSdkVersion 属性导致它为我而改变。

标签: android colors tabs themes tabwidget


【解决方案1】:

由于 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&lt;application /&gt; 元素中,即可将该主题应用于我的应用程序。

【讨论】:

  • thanx steve,它帮助了我,你成就了我的生活
【解决方案2】:

查看我的这个答案:Background in tab widget ignore scaling

也可以参考android.graphics.drawable

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

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

【讨论】:

  • 这无论如何都会使标签背景变黑。
【解决方案3】:

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
}

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      相关资源
      最近更新 更多