2012-02-20 16 views
7

Hola tengo dos pestañas en mi widget de pestañas, quiero aplicar los dos colores diferentes para dos pestañas.am buscando en todas partes, la mayoría de los colores son los mismos al aplicar la pestaña.¿Es posible cambiar el color de la pestaña seleccionada en Android?

actualización

primera pestaña cuando se selecciona el rojo

segunda pestaña cuando se selecciona el color azul

Aquí mi código

tabHost = (TabHost)findViewById(android.R.id.tabhost); 
    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");//these are color red 
    TabSpec secondTabSpec = tabHost.newTabSpec("tid1");//these color blue 
    firstTabSpec.setIndicator("Sales Info",getResources().getDrawable(R.drawable.sales)); 
    Intent photosIntent = new Intent(this, a.class); 
    firstTabSpec.setContent(photosIntent); 
    secondTabSpec.setIndicator("Service Info",getResources().getDrawable(R.drawable.services)); 
    Intent photosIntent1 = new Intent(this, b.class); 
    secondTabSpec.setContent(photosIntent1); 
    tabHost.addTab(firstTabSpec); 
    tabHost.addTab(secondTabSpec); 

Respuesta

12

Prueba esto:

...onCreate(){ 

    ... 
    tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

    @Override 
    public void onTabChanged(String arg0) { 

     setTabColor(tabHost); 
    } 
    }); 
    setTabColor(tabHost); 
... 
} 

//Change The Backgournd Color of Tabs 
public void setTabColor(TabHost tabhost) { 

    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
     tabhost.getTabWidget().getChildAt(i).setBackgroundColor(COLOR_CYAN); //unselected 

    if(tabhost.getCurrentTab()==0) 
      tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_RED); //1st tab selected 
    else 
      tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_BLUE); //2nd tab selected 
} 
+0

por favor actualice mi pregunta – Mercy

+0

@micro: por favor vea mi actualización responder. – Hiral

+0

Gracias por su funcionamiento, pero aplico el código de color Color.green (0xCFEB5D) en lugar de (color.GREEN). ¿No funciona? ¿Por qué? – Mercy

7

Puede establecer Listener para su TabHost usando setOnTabChangedListener y cambiar de forma dinámica,

public void onCreate(Bundle savedInstanceState){ 
    // add your tabs here 

    // set the First Tab as selected Tab. 
    setSelectedTabColor(); 
} 

crear un método que va a establecer el Selected y Unselected color del Tab.

private void setSelectedTabColor() { 
     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
     { 
      tabHost.getTabWidget().getChildAt(i) 
              .setBackgroundColor(Color.WHITE); 
     } 
     tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()) 
               .setBackgroundColor(Color.RED); 
    } 

Luego, dentro de su onTabChanged() puede cambiar dinámicamente el fondo.

@Override 
    public void onTabChanged(String tabId) { 
     setSelectedTabColor(); 
    } 

se puede utilizar el mismo para selected y unselected Tab, here es el blog para el mismo.

+0

Gracias, Tipo no coinciden: no se puede convertir de vacío a Ver am getting error View view = myTabHost.getTabWidget(). GetChildAt (tab) .setBackgroundColor (Color.CYAN); esta línea – Mercy

+0

Una buena idea para levantarse .... ¿Qué hay de las pestañas no seleccionadas? –

+0

@lalit por favor actualice mi pregunta – Mercy

2

Utilice la setIndicator (Ver vista) en lugar de setIndicator (etiqueta CharSequence, icono Disponibles). La configuración de fondo de la vista que aprobará (por ejemplo, si está inflando un xml el diseño principal) debe ser una ColorStateList para manejar los clics.

Cuestiones relacionadas