java 如何更改 SlidingTabLayout 的文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25089866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:19:23  来源:igfitidea点击:

How to change the text color of SlidingTabLayout?

javaandroidandroid-actionbarandroid-viewpagerandroid-actionbar-compat

提问by seba123neo

I made an application which use the ActionBarCompat

我制作了一个使用ActionBarCompat的应用程序

I created the tabs using the SlidingTabLayoutclass.

我使用SlidingTabLayout类创建了选项卡。

the class is this:

这门课是这样的:

SlidingTabLayout.java

SlidingTabLayout.java

but I can not change the color of the tabs...

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

my viewpager fragment is this:

我的 viewpager 片段是这样的:

<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 textof the tabs...

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

I made the application after seeing the following example:

我在看到以下示例后制作了应用程序:

rudsonlive/Navigation-Drawer-ViewPager-ActionBarCompat

rudsonlive/Navigation-Drawer-ViewPager-ActionBarCompat

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

如何更改选项卡文本的文本颜色?

thanks !!!

谢谢 !!!

回答by Panayiotis Irakleous

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

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

<?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) Then in the populateTabStrip() method in SlidingTabLayout put this

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

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

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

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

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

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

and b) change the onPageSelected() method to this

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

    @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);
        }
    }    

回答by TheOddAbhi

Open your file SlidingTabLayout.java(the default one from Google IO) and find the function populateTabStrip(), then after this code

打开您的文件SlidingTabLayout.java(Google IO 的默认文件)并找到函数populateTabStrip(),然后在此代码之后

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

add the following line:

添加以下行:

int color = ContextCompat.getColor(tabView.getContext(), R.color.grey);
tabTitleView.setTextColor(color);

Replace R.color.greywith your preferred color.

替换R.color.grey为您喜欢的颜色。

回答by Aman Verma

copy code of slidingtablayout and slidingtabstrip and put it in a java file.then make a customtab_title.xml in your layout folder and a selector.xml file in your drawable folder. `

复制slidingtablayout 和slidingtabstrip 的代码并将其放入java 文件中。然后在布局文件夹中创建customtab_title.xml,在drawable 文件夹中创建一个selector.xml 文件。`

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


<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:textColor="@drawable/slidingtab_title_color"/>


</LinearLayout>

selector.xml

选择器.xml

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

And in your mainactivity or where u r showing your tabs add one line of code - tabs.setCustomTabView(R.layout.customtab_title, R.id.textView2);

在您的主要活动或您显示选项卡的地方添加一行代码 - tabs.setCustomTabView(R.layout.customtab_title, R.id.textView2);

here tabs is slidingtablayout tabs;

这里的标签是slidingtablayout标签;

to change indicator color add - tabs.setSelectedIndicatorColors(getResources().getColor(R.color.unpressed));

更改指示器颜色添加 - tabs.setSelectedIndicatorColors(getResources().getColor(R.color.unpressed));

回答by MrLithid

You should be able to see the TextView the class is using.

您应该能够看到该类正在使用的 TextView。

tabTitleView.setTextColor(getResources().getColor(R.color.white));

In my class, the TextView was tabTitleView. If you are using the default example provided by Google, you will find it under populateTabStrip function.

在我的课堂上,TextView 是 tabTitleView。如果您使用的是 Google 提供的默认示例,您会在 populateTabStrip 函数下找到它。

回答by Vinod Ranga

   @Override
    public void onPageSelected(int position) {

for (int i = 0; i < mTabStrip.getChildCount(); i++) {

  TextView tv = (TextView) mTabStrip.getChildAt(i);
 if (i==position)
  tv.setTextColor(getResources().getColorStateList(R.color.white));
 else                             
  tv.setTextColor(getResources().getColorStateList(R.color.tab_text_color));

        }

this may be help you

这可能对你有帮助

回答by BladeCoder

Unfortunately this class doesn't support customizing the tab text color without editing the code and always uses the default text color of the theme. You'll have to patch the class to allow setting the tabs text color by code or by style attribute. One alternative is to use the PagerSlidingTabStriplibrary.

不幸的是,此类不支持在不编辑代码的情况下自定义选项卡文本颜色,并且始终使用主题的默认文本颜色。您必须修补该类以允许通过代码或样式属性设置选项卡文本颜色。一种替代方法是使用PagerSlidingTabStrip库。

回答by simplatek

Looking at the code for the SlidingTabLayout...You can set a custom tab view, which allows you to control the content of the tab and set a custom tab text color. Have a look at slidingTabLayout.setCustomTabView(int layoutResId, int textViewId).

查看 SlidingTabLayout 的代码...可以设置自定义选项卡视图,它允许您控制选项卡的内容并设置自定义选项卡文本颜色。看看slidingTabLayout.setCustomTabView(int layoutResId, int textViewId)。

回答by saggaf.arsyad

I use Panayiotis Irakleous solution but I think it is better to avoid looping part in onPageSelected procedure.

我使用 Panayiotis Irakleous 解决方案,但我认为最好避免在 onPageSelected 过程中循环部分。

The steps are the same, you need to add an int class member (example: mCurrentTabIndex) to save current tab index.

步骤相同,需要添加一个 int 类成员(例如:)mCurrentTabIndex来保存当前选项卡索引。

In steps 3.a, you need to add

在步骤 3.a 中,您需要添加

mCurrentTabIndex = i;

So it will be like this:

所以它会是这样的:

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

Last, in steps 3.b, replace the looping part to this:

最后,在步骤 3.b 中,将循环部分替换为:

mTabStrip.getChildAt(mCurrentTabIndex).setSelected(false);
mTabStrip.getChildAt(position).setSelected(true);
mCurrentTabIndex = position;

So the code will be like this:

所以代码会是这样的:

@Override
public void onPageSelected(int position) {
    if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
        mTabStrip.onViewPagerPageChanged(position, 0f);
        scrollToTab(position, 0);
    }

    mTabStrip.getChildAt(mCurrentTabIndex).setSelected(false);
    mTabStrip.getChildAt(position).setSelected(true);
    mCurrentTabIndex = position;

    if (mViewPagerPageChangeListener != null) {
        mViewPagerPageChangeListener.onPageSelected(position);
    }
}