How to change the text color of SlidingTabLayout?(如何更改 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);
}
}
这篇关于如何更改 SlidingTabLayout 的文字颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改 SlidingTabLayout 的文字颜色?


基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01