android android-widget

Android: Cambiar el color del texto de la pestaña mediante programación



android-widget (4)

Tengo un TabHost como este:

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabhost" android:background="@drawable/tabs_bg"> <LinearLayout android:id="@+id/LinearLayout01" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginBottom="5dip"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_width="fill_parent"> </FrameLayout> </LinearLayout>

Y estoy agregando pestañas a este TabHost programáticamente de esta manera:

tabHost = (TabHost)findViewById(android.R.id.tabhost); tabHost.setOnTabChangedListener(this); /* tid1 is firstTabSpec Id. Its used to access outside. */ TabSpec firstTabSpec = tabHost.newTabSpec("tid1"); TabSpec secondTabSpec = tabHost.newTabSpec("tid2"); TabSpec ThirdTabSpec = tabHost.newTabSpec("tid3"); /* TabSpec setIndicator() is used to set name for the tab. */ /* TabSpec setContent() is used to set content for a particular tab. */ firstTabSpec.setIndicator("Tab1", getResources().getDrawable(R.drawable.tab1)); secondTabSpec.setIndicator("Tab2", getResources().getDrawable(R.drawable.tab2)); ThirdTabSpec.setIndicator("Tab3", getResources().getDrawable(R.drawable.tab3)); firstTabSpec.setContent(new Intent(this,FirstTab.class)); secondTabSpec.setContent(new Intent(this,SecondTab.class)); ThirdTabSpec.setContent(new Intent(this,ThirdTab.class)); /* Add tabSpec to the TabHost to display. */ tabHost.addTab(firstTabSpec); tabHost.addTab(secondTabSpec); tabHost.addTab(ThirdTabSpec); for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) { tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#121312")); } tabHost.getTabWidget().setCurrentTab(0); tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#f1a026"));

Y aquí está el evento onTabChanged:

public void onTabChanged(String tabId) { // TODO Auto-generated method stub for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) { tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#121312")); } tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#f1a026")); }

En el evento onTabChanged, también quiero cambiar el color del texto de todas las pestañas. Por favor, ayúdame, ¿cómo puedo cambiar el color del texto de las pestañas en el evento?

Gracias,


Cuando usa findViewById (id), le pide al sistema que busque cualquier Vista con el id "id" en relación con el ViewGroup actual. Eso significa que lo que haces en tu código es este .findViewById (id), por lo que buscará "id" en la vista actual. Y hacer findViewById (android.R.id.tabHost) no es muy inteligente porque simplemente no existe ...

Sin embargo, cuando obtiene getTabHost (), le pide al sistema que obtenga el único tabHost de su actividad, sin importar que tenga una vista como raíz, es decir, el tabHost no puede asociarse con nada.

Como conclusión, siempre deberías usar TabHostActivity en tu TabHostActivity

Espero que haya sido claro


Para cambiar el color del texto de las pestañas, necesita obtener la vista, es decir, TextView, que se establece como título de las pestañas, y puede cambiarlo de esta manera:

TabHost tabhost = getTabHost(); for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) { TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); tv.setTextColor(.....); }

espero que esto ayude....


Para el nuevo diseño de la pestaña de soporte de diseño; Puedes definirlo en tu xml.
app:tabTextColor="@color/tab_inactive" app:tabSelectedTextColor="@color/tab_active"
P.ej -

<android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tabanim_tabs" app:tabTextColor="@color/tab_inactive" app:tabSelectedTextColor="@color/tab_active" android:textAllCaps="false" />


Programáticamente se puede lograr así:

tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.tab_selector)); tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator));


Para mí, la solución de @Farhan no funcionó ya que getChildCount() siguió devolviendo 1 mientras tenía cuatro pestañas. Usando getTabCount() y getChildTabViewAt() resolví por mí:

for (int tabIndex = 0 ; tabIndex < mTabHost.getTabWidget().getTabCount() ; tabIndex ++) { View tab = mTabHost.getTabWidget().getChildTabViewAt(tabIndex); TextView t = (TextView)tab.findViewById(android.R.id.title); t.setTextColor(getResources().getColor(R.color.white)); }

Pensé que publicaría esta alternativa para las personas que tienen el mismo problema.