Custom selected tab text color in SlidingTabLayout

前端 未结 6 1992
执笔经年
执笔经年 2020-12-13 14:14

I\'m using the SlidingTabLayout from google (https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html).

It wo

6条回答
  •  时光取名叫无心
    2020-12-13 15:00

    I had a similar problem but I was using a custom page title View with an icon and a text. To set custom colors when a tab is selected/unselected, I used the selector created by @PanayiotisIrakleous, so a big thanks to him for posting it.

    Here's how I did it:-

    1- Create a xml file for the selector. I made a file, slidingtab_title_color.xml and placed it in Drawable folder.

    
    
        
        
        
        
     
    

    2- Open your custom layout for tab title and add the selector file in the android:textColor attribute. My custom file is named as slidingtab_title_color.xml and has the following code-

    
    
        
    
            
    
    

    3- (Optional) If you want to change the color of the indicator and the background of sliding tab then add the following line to the file where you are initializing your SlidingTabLayout-

    mSlidingTab.setBackgroundColor(getResources().getColor(R.color.primaryColor));
    mSlidingTab.setSelectedIndicatorColors(getResources().getColor(R.color.accentColor));
    

    Just make sure that you are adding these lines before you are setting the ViewPager for the SlidingTabLayout.

    And that's it, here's how it will look.SlidingTab with tab selector

    For those who still have problems, here's the bitbucket link for the project source and this link for the all the projects on Material design.

提交回复
热议问题