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 的文字颜色?


- Jersey REST 客户端:发布多部分数据 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01