TabWidget color de primer plano blanco?
Pregunta
No sé lo que hice, pero por un período de tiempo que mi TabWidget tenido pestañas de color blanco, que parecía muy agradable. Nunca me puse un tema o de fondo / color de primer plano en mi proyecto en absoluto. La próxima vez que compilé que se volvió de nuevo a las lengüetas grises. Mi aplicación está utilizando el tema por defecto oscuro. Incluso si fijo el tema de la aplicación a la luz, las pestañas siguen siendo gris. Así que, obviamente, que era otra cosa que cambió el color de las pestañas. Alguien sabe cómo hacer esto?
Solución
Yo estaba teniendo un problema debido a un error en Android 1.6 es el tema de la luz (texto indicador de tabulación es blanco). Yo era capaz de anular el tema por defecto de la siguiente manera:
- He creado un tema personalizado que hereda del tema por defecto:
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>
Entonces sólo se aplica ese tema a mi solicitud añadiendo android:theme="@style/MyTheme"
al elemento <application />
de mi AndroidManifest.xml
.
Otros consejos
Comprobar esta respuesta mía: en la pestaña Widget ignorar escalar
También puede referirse a la android.graphics.drawable
paquete
En el código, se puede establecer el fondo para sus pestañas como esta:
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(
android.R.color.white);
en la public void onCreate(Bundle savedInstanceState)
`tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
tabHost.setCurrentTab(0);
setTabColor();`
que en el oyente:
onTabChanged public void (String tabid) { setTabColor ();
Por último la función, que establece el primer plano y el fondo también:
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
}
En 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);
}
}