自定义选择标签的文本颜色SlidingTabLayout [英] Custom selected tab text color in SlidingTabLayout

查看:442
本文介绍了自定义选择标签的文本颜色SlidingTabLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是SlidingTabLayout从谷歌(<一href="https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html">https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html).

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

它运作良好,但我想这是把所选标题加粗并用不同的颜色...

It works well, but what I want it is to put the selected title in bold and with a different color...

关于这篇文章: <一href="http://stackoverflow.com/questions/25568392/custom-unselected-tab-text-color-in-slidingtablayout">Custom在SlidingTabLayout未选定标签的文本颜色

我做一个text_tab.xml的绘制与选择:

I make a text_tab.xml in drawable with the selector:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:color="@android:color/selected" android:state_selected="true" />
 <item android:color="@android:color/unselected" />
 </selector>

当在populateTabStrip()方法,我把

When in the populateTabStrip() method I put

 tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.text_tab));

的颜色总是未选定一...

The color is always the one of unselected...

我可能做错了什么,或者还有另一种方式来定制所选择的选项卡的标题。

I'm probably doing something wrong, or maybe there is another way to customise the selected tab title.

有人有一个想法?

感谢

推荐答案

现在的问题是,滑动布局不设置项的状态选择。这里是我的方法来解决这个问题。

The problem is, sliding layout do not set item's state as selected. Here is my approach to solve the problem.

1)创建的颜色选择器( Col​​orStateList )为您的看法。你可以想象这样说:

1) Create COLOR selector (ColorStateList) for your view. You can imagine it this way:

/res/color/tab_text_color.xml

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

2)将创建的选择你的项目的看法文字颜色(或其他所需的)的属性:

2) Place created selector to your item's view textColor (or other required) attribute:

<TextView
  ...
  android:textColor="@color/tab_text_color"
  ... />

3)操作方式的改变文件SlidingTabLayout.java:

3) Do this changes in file SlidingTabLayout.java:

View oldSelection = null; // new field indicating old selected item

// method to remove `selected` state from old one
private void removeOldSelection() { 
    if(oldSelection != null) {
        oldSelection.setSelected(false);
    }
}

// improve method scrollToTab() as follows
private void scrollToTab(int tabIndex, int positionOffset) {
    final int tabStripChildCount = mTabStrip.getChildCount();
    if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
        return;
    }

    View selectedChild = mTabStrip.getChildAt(tabIndex);
    if (selectedChild != null) {

        if(positionOffset == 0 && selectedChild != oldSelection) { // added part
            selectedChild.setSelected(true);
            removeOldSelection();
            oldSelection = selectedChild;
        }

        int targetScrollX = selectedChild.getLeft() + positionOffset;

        if (tabIndex > 0 || positionOffset > 0) {
            // If we're not at the first child and are mid-scroll, make sure we obey the offset
            targetScrollX -= mTitleOffset;
        }

        scrollTo(targetScrollX, 0);
    }
}

private void populateTabStrip() {
    removeOldSelection(); // add those two lines
    oldSelection = null;
    ...
}

这篇关于自定义选择标签的文本颜色SlidingTabLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆