TabLayout tabs texto que no se muestra

Estoy utilizando TabLayout dentro de un Fragment para mostrar tres pestañas fijas y las pestañas están trabajando, pero no muestra el texto de la pestaña, incluso después de que establezca el atributo app:tabTextColor en el diseño que todavía no está visible.

NewFragment.java

 public class NewFragment extends Fragment { private RecyclerView mRecyclerView; private RecyclerView.LayoutManager mLayoutManager; private RecyclerView.Adapter mAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.new_fragment, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); viewPager.setAdapter(new PagerAdapter(getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setupWithViewPager(viewPager); tabLayout.setTabMode(TabLayout.MODE_FIXED); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Override public Fragment getItem(int position) { switch (position) { case 0: return new FragmentTab(); case 1: return new FragmentTab(); case 2: return new FragmentTab(); default: return null; } } @Override public int getCount() { return mNumOfTabs; } } } 

Newfragment.xml

 <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".NewFragment"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="fixed" app:tabTextColor="#ffffff" app:tabGravity="fill" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"/> </android.support.design.widget.AppBarLayout> 

El problema es que estás llamando a setupWithViewPager() después de configurar tus pestañas con las llamadas addTab() , sobrescribiéndolas efectivamente.

De la documentación de TabLayout respecto a setupWithViewPager() :

Las pestañas mostradas en esta presentación se rellenarán a partir de los ViewPager la página del adaptador ViewPager .

Si desea utilizar su TabLayout con un ViewPager , debe anular getPageTitle() en su PagerAdapter (y eliminar las llamadas addTab() , son redundantes).

Por ejemplo:

 public class ExamplePagerAdapter extends FragmentStatePagerAdapter { // tab titles private String[] tabTitles = new String[]{"Tab1", "Tab2", "Tab3"}; public ExamplePagerAdapter(FragmentManager fm) { super(fm); } // overriding getPageTitle() @Override public CharSequence getPageTitle(int position) { return tabTitles[position]; } @Override public Fragment getItem(int position) { switch (position) { case 0: return new Tab1Fragment(); case 1: return new Tab2Fragment(); case 2: return new Tab3Fragment(); default: return null; } } @Override public int getCount() { return tabTitles.length; } } 

I agregó texto de pestaña e icono para cada pestaña después de llamar a tabs.setupWithViewPager(viewPager)

  viewPager.setAdapter(new MyViewAdapter(getSupportFragmentManager())); TabLayout tabs=(TabLayout) findViewById(R.id.tabs); tabs.setupWithViewPager(viewPager); tabs.getTabAt(0).setIcon(R.drawable.icon1); tabs.getTabAt(1).setIcon(R.drawable.icon2); tabs.getTabAt(0).setText(getResources().getText(R.string.tab1)); tabs.getTabAt(1).setText(getResources().getText(R.string.tab2)); 
  • TabMode diferente para TabLayout
  • Definir el estilo TabLayout en el tema
  • Quite la sombra debajo de TabLayout en android
  • Android TabLayout no muestra contenido tan pronto como el fragmento se cambia
  • Se ha producido un error al inflar la clase android.support.design.widget.TabLayout
  • Programe ocultar / mostrar el diseño de soporte de Android Android TabLayout dentro de AppBarLayout
  • TabLayout con ViewPager no funciona dentro de fragmento Android
  • TabLayout dentro de fragmento da mala vista de elementos
  • Android: textAllCaps = "false" no funciona para el diseño de TabLayout
  • Cómo desplazar tablayout programatically - Android
  • Deshabilitar tabulaciones en TabLayout
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.