如何更改 SlidingTabLayout 的文字颜色?

我做了一个使用 ActionBarCompat 的应用程序

I made an application which use the ActionBarCompat

我使用 SlidingTabLayout 类创建了标签.

I created the tabs using the SlidingTabLayout class.

课程是这样的:

SlidingTabLayout.java

但我无法更改标签的颜色...

我的 viewpager 片段是这样的:

my viewpager fragment is this:

<swmovil.fyb.SlidingTabLayout
    android:id="@+id/mTabs"
    android:layout_width="match_parent"
    android:layout_height="48dip" />

<android.support.v4.view.ViewPager
    android:id="@+id/mPager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@color/white" />

该应用程序运行良好,但我无法更改选项卡的彩色文本...

the application works great, but i can´t change the color text of the tabs...

看到下面的例子,我就申请了:

I made the application after seeing the following example:

rudsonlive/Navigation-Drawer-ViewPager-ActionBarCompat

如何更改标签文本的文本颜色?

How can i change the text color of the tabs text ?

谢谢!!!

推荐答案

1) 首先在res(/res/color)下创建color文件夹
2) 在/res/color文件夹下创建xml文件selector.xml

1) First of all create color folder under res (/res/color)
2) create xml file selector.xml under /res/color folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#504f4f" /> 
</selector> 

3) 然后在SlidingTabLayout的populateTabStrip()方法中放这个

3) Then in the populateTabStrip() method in SlidingTabLayout put this

tabTitleView.setTextColor(getResources().getColorStateList(R.color.selector));

现在你有了一个选择器,你可以在任何你想要的事件上更改文本的颜色

now you have a selector and you can change the color of the text on any event you want

如果这不起作用,请添加以下代码行.
a) 在最后的 populateTabStrip() 方法中添加这个

if that is not working add the following lines of code.
a) in populateTabStrip() method at the end add this

if (i == mViewPager.getCurrentItem()) {
    tabView.setSelected(true);
}

和 b) 将 onPageSelected() 方法更改为此

and b) change the onPageSelected() method to this

    @Override
    public void onPageSelected(int position) {
        if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
            mTabStrip.onViewPagerPageChanged(position, 0f);
            scrollToTab(position, 0);
        }
        for (int i = 0; i < mTabStrip.getChildCount(); i++) {
            mTabStrip.getChildAt(i).setSelected(position == i);
        }
        if (mViewPagerPageChangeListener != null) {
            mViewPagerPageChangeListener.onPageSelected(position);
        }
    }    

相关文章