更改SlidingTabLayout中的颜色指示器

时间:2014-11-30 05:10:52

标签: android

我想问一下,是否可以更改SlidingTablayout中标签指示符的颜色?我必须使用developer.android.com的SlidingTabsColors吗?我只是想换另一种颜色而不是默认的蓝色(我认为)颜色。请指教。感谢!!!

2 个答案:

答案 0 :(得分:14)

只是为了更清楚。

SlidingTabLayout tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs); //referring the layout in xml file
tabs.setViewPager(viewpager);    //setting the viewpager


//setting indicator and divider color
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {

    @Override
    public int getIndicatorColor(int position) {
    return getResources().getColor(R.color.white);    //define any color in xml resources and set it here, I have used white
    }

    @Override
    public int getDividerColor(int position) {
    return getResources().getColor(R.color.white);
    }
});

答案 1 :(得分:2)

正如您在源代码中看到的那样,您必须在接口

下面实现
/**
 * Allows complete control over the colors drawn in the tab layout. Set with
 * {@link #setCustomTabColorizer(TabColorizer)}.
 */
public interface TabColorizer {

    /**
     * @return return the color of the indicator used when {@code position} is selected.
     */
    int getIndicatorColor(int position);

    /**
     * @return return the color of the divider drawn to the right of {@code position}.
     */
    int getDividerColor(int position);

}

并通过从mSlidingTabLayout

调用以下方法来设置它
/**
 * Set the custom {@link TabColorizer} to be used.
 *
 * If you only require simple custmisation then you can use
 * {@link #setSelectedIndicatorColors(int...)} and {@link #setDividerColors(int...)} to achieve
 * similar effects.
 */
public void setCustomTabColorizer(TabColorizer tabColorizer) {
    mTabStrip.setCustomTabColorizer(tabColorizer);
}

或者你可以改变

private static final int DEFAULT_SELECTED_INDICATOR_COLOR = 0xFFF49e04;

来自 SlidingTabStrip 类。

编辑:

您的主要活动或您想要控制颜色的任何对象必须在界面下方实现:

public class MainActivity extends FragmentActivity implements SlidingTabLayout.TabColorizer

然后在覆盖方法中根据位置选择颜色:

@Override
public int getIndicatorColor(int position) {

    return (Your color value );
}


@Override
public int getDividerColor(int position) {
    return (Your color value );
}

然后你必须将该对象传递给SlidingTab。