代码之家  ›  专栏  ›  技术社区  ›  Monstieur

tabwidget白色前景色?

  •  6
  • Monstieur  · 技术社区  · 14 年前

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

    4 回复  |  直到 11 年前
        1
  •  15
  •   Steve Pomeroy    14 年前

    我遇到了一个问题,因为android 1.6的light主题中有一个bug(tab indicator文本是白色的)。我可以重写默认主题,如下所示:

    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 .

        2
  •  6
  •   Community CDub    7 年前

    检查我的答案: Background in tab widget ignore scaling

    你也可以参考 android.graphics.drawable 包裹

    在代码中,可以设置选项卡的背景,如下所示:

    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(
                android.R.color.white);
    
        3
  •  1
  •   user529543    13 年前

    public void onCreate(Bundle savedInstanceState)

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

    在听众中:

    public void 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
    }
    
        4
  •  0
  •   Milan    11 年前

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