1

Actuellement, j'essaie de style un Tab dans un TabLayout différemment au reste. Je voudrais un Tab avec un index spécifique pour avoir du texte rouge, à la fois sélectionné et non sélectionné. Comme je ne nécessitant que pour un seul Tab à un index spécifique, la solution habituelle ci-dessous ne fonctionne pas:Modifier la couleur du texte sélectionné/non sélectionné d'un onglet spécifique dans un TabLayout avec un index (seul style un onglet)

app:tabSelectedTextColor="@color/red" 
app:tabTextColor="@color/red" 

J'ai aussi tenté d'appliquer un Spannable lors du retour du titre pour cette Tab spécifique, mais ce doesn « t afficher réellement:

@Override 
public CharSequence getPageTitle(int position) { 
    switch (position) { 
     case TAB_ONE: 
      return getString(R.string.tab_one); 
     case TAB_TWO_RED: 
      Spannable spannable = new SpannableString(getString(R.string.tab_two)); 
      spannable.setSpan(new ForegroundColorSpan(Color.RED), 
         0, getString(R.string.tab_two).length(), 
         Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      return spannable; 
     } 
     return null; 
    } 

Je vous serais reconnaissant grandement si quelqu'un savait comment le style du texte pour une Tab avec un index spécifié uniquement.

Répondre

2

C'est assez simple.

essayez de définir une customView à cet onglet spécifique dans le TabLayout comme suit:

TabLayout tabLayout; 
tabLayout = (TabLayout) findViewById(R.id.pTabs); 
tabLayout.getTabAt(1).setCustomView(R.layout.tab_two_layout); 

Votre XML pour la mise en page personnalisée (R.layout.tab_two_layout) ressemblerait à quelque chose comme ceci:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:textStyle="bold" 
     android:layout_marginLeft="5dp" 
     android:layout_marginStart="5dp" 
     android:layout_width="60dp" 
     android:textColor="@color/red" 
     android:layout_gravity="center_vertical" 
     android:text="TEXT HERE" 
     android:layout_height="wrap_content"/> 

</LinearLayout>