Question

Je ne sais pas ce que je faisais, mais pendant une période de temps mon TabWidget avait des onglets de couleur blanche qui avait l'air vraiment agréable. Je ne ai jamais mis un thème ou d'arrière-plan / couleur de premier plan dans mon projet tout. La prochaine fois que je compilé il est revenu à pattes grises. Mon application utilise le thème par défaut sombre. Même si je mets le thème de l'application à la lumière, les onglets sont toujours gris. Alors, évidemment, il était autre chose qui a changé la couleur des onglets. Quelqu'un sait comment faire cela?

Était-ce utile?

La solution

J'avais un problème dû à un bogue dans le thème de la lumière Android 1.6 (onglet texte indicateur est blanc). J'ai pu remplacer le thème par défaut comme suit:

  1. J'ai créé un thème personnalisé qui a hérité du thème par défaut:

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>

Alors je demande juste ce thème à ma demande en ajoutant android:theme="@style/MyTheme" à l'élément <application /> de mon AndroidManifest.xml.

Autres conseils

Cochez cette réponse de mes: widget dans l'onglet ignorer mise à l'échelle

Vous pouvez également consulter la page android.graphics.drawable paquet

Dans votre code, vous pouvez définir l'arrière-plan pour vos onglets comme ceci:

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

dans le public void onCreate(Bundle savedInstanceState)

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

que dans l'auditeur:

public void onTabChanged (String Tabid) {         setTabColor ();

enfin la fonction, trop au premier plan et l'arrière-plan:

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
}

Dans 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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top