2016-07-16 4 views
0

Voici ma mise en page de activity_custom_view_icon_text_tabs.xml:Comment définir la couleur pour le texte et l'icône d'un onglet dans TabLayout quand il se concentrer

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/custom_tab_layout_height" 
     app:tabMode="fixed" 
     app:tabGravity="fill"/> 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

Voici mon code pour activity_custom_view_icon_text_tabs.xml: http://pastebin.com/raw/h0AN6Mtj

custom_tab.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/tab" 
android:textColor="@color/colorAccent" 
android:textSize="@dimen/tab_label" 
android:fontFamily="@string/font_fontFamily_medium"/> 
+0

vous pouvez utiliser sélecteur drawable sur textview, comme http://stackoverflow.com/questions/5624609/android-how-to-make-a-drawable-selector –

+0

utilisez ceci, je dois avoir à la fois des icônes sélectionnées et non sélectionnées dans drawable, y a-t-il autre chose? –

+0

c'est un meilleur moyen. ou vous pouvez manipuler la vue personnalisée sur la sélection de tabulation. –

Répondre

1

Je cours ce code dans mon exemple et cela fonctionne très bien.

Vous devez apporter quelques modifications décrites dans l'exemple suivant.

pour le fond sélecteur onglet sous res/étirables/background_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@color/colorAccent" android:state_selected="true" /> 
    <item android:drawable="@color/tabcolor" /> 
</selector> 

et pour la couleur de sélection de texte sous res/couleur/background_text_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@android:color/holo_red_dark" android:state_selected="true" /> 
    <item android:color="@android:color/holo_green_dark" /> 
</selector> 

et votre textview est quelque chose comme ceci:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background_selector" 
    android:textColor="@color/background_text_selector"> 
</TextView> 

références enlevèrent pour sélecteur de couleur:

https://developer.android.com/guide/topics/resources/color-list-resource.html

et sélecteur drawable:

https://developer.android.com/guide/topics/resources/drawable-resource.html

de ce lien.

+0

merci mon cher, c'est vraiment utile .. –