SlidingTabLayout中如何更改颜色指示器?

6

我想问一下,在SlidingTabLayout中是否可以更改选项卡指示器的颜色?我是否必须使用来自developer.android.com的SlidingTabsColors?我只想将另一种颜色替换默认的蓝色(我觉得是)。请给予建议。谢谢!!!

2个回答

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

如何设置未选中标签指示器的颜色 - Aman Verma

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。


你能给我一个例子吗?不改变DEFAULT_SELECTED_INDICATOR_COLOR。 - DanKCl
我对这段代码非常好奇,但现在没有电脑来测试它。所以我想问你:这个TabColorizer接口是否实现了颜色之间的淡入淡出效果,还是从一个淡入淡出到另一个是突变的? - Joaquin Iurchuk
不幸的是,我无法记住它是否已经实现了,但最终我认为你可以使用 objectAnimator 来实现它。 - mmlooloo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接