Cómo configurar el color de fondo TabHost

Necesito ayuda, estoy encontrando dificultad para cambiar el color de fondo en un TabHost.

Imagen original:

Image1

Necesito modificar el color de fondo como imagen abajo.

Image2

He intentado muchas cosas en mi código y XML también, pero no.

Mi código abajo:

TabHost tabHost = getTabHost(); // Tab 1 TabSpec aba1spec = tabHost.newTabSpec("Tab 1"); // setting Title and Icon for the Tab tabHost.getTabWidget().setStripEnabled(false); aba1spec.setIndicator("",getResources().getDrawable(R.drawable.tabenviaarq)); Intent photosIntent = new Intent(this, MainActivity.class); aba1spec.setContent(photosIntent); // Adding all TabSpec to TabHost tabHost.addTab(aba1spec); // Adding tab1 

En XML tengo esto:

 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@android:id/tabs" android:layout_alignParentTop="true"/> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="65dp" android:layout_alignParentBottom="true" android:layout_marginBottom="-5dp" android:background="#000000"/> </RelativeLayout> </TabHost> 

Alguien tiene una idea que muchas gracias.

 tabHost.setOnTabChangedListener(new OnTabChangeListener() { public void onTabChanged(String arg0) { for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) { tab.getTabWidget().getChildAt(i) .setBackgroundResource(R.drawable.tab_selected); // unselected } tab.getTabWidget().getChildAt(tab.getCurrentTab()) .setBackgroundResource(R.drawable.tab_unselected); // selected } }); 

Pruebe este método, espero que esto le ayudará.

La solución es usar el fondo con el selector, y el código está aquí:

 private void initTabsAppearance(TabWidget tabWidget) { // Change background for(int i=0; i < tabWidget.getChildCount(); i++) tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg); } 

Donde tab_bg es un xml dibujable con el selector:

Para la personalización completa de la pestaña, añadiré el código para cambiar el estilo del texto de la pestaña mediante el tema personalizado. Agregue esto a styles.xml:

 <style name="MyCustomTheme" parent="@android:style/Theme.Light.NoTitleBar"> <item name="android:tabWidgetStyle">@style/CustomTabWidget</item> </style> <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget"> <item name="android:textAppearance">@style/CustomTabWidgetText</item> </style> <style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget"> <item name="android:textSize">12sp</item> <item name="android:textStyle">bold</item> </style> 

Para utilizar este tema, defínalo en AndroidManifest.xml:

 <application android:theme="@style/MyCustomTheme"> 

Y ahora tienes widgets de pestañas con un fondo personalizado y un estilo de texto personalizado.

Estoy resuelto exactamente el mismo problema con este método:

 private void setBackgroundColor() { int inactiveColor = getResources().getColor(R.color.inactive_tab); int activeColor = getResources().getColor(R.color.active_tab); // In this loop you will set the inactive tabs backgroung color for (int i = 0; i < tabWidget.getChildCount(); i++) { tabWidget.getChildAt(i).setBackgroundColor(inactiveColor); } // Here you will set the active tab background color tabWidget.getChildAt(tabHost.getCurrentTab()).setBackgroundColor( activeColor); } 
  • Comportamiento extraño con selectAllOnFocus cuando EditText está en ListView
  • OpenCV Android - edición de color usando CameraBridgeViewBase
  • CSVReader y InputStream
  • Usar putExtra para pasar valores al servicio de intenciones
  • Android-transparent RelativeLayout
  • Cómo pasar la intención con extras a una actividad ya en ejecución
  • Botón de Android onClickListener
  • Fragmentos Tipos incompatibles
  • Vínculos con varios clics en TextView en Android
  • ¿Es posible cambiar la fuente de entrada de TV de forma programática?
  • Import slidingmenu library en Eclipse Indigo Service Release 2
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.