我想问一下,在SlidingTabLayout中是否可以更改选项卡指示器的颜色?我是否必须使用来自developer.android.com的SlidingTabsColors?我只想将另一种颜色替换默认的蓝色(我觉得是)。请给予建议。谢谢!!!
我想问一下,在SlidingTabLayout中是否可以更改选项卡指示器的颜色?我是否必须使用来自developer.android.com的SlidingTabsColors?我只想将另一种颜色替换默认的蓝色(我觉得是)。请给予建议。谢谢!!!
只是为了更加清晰明了。
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);
}
});
如您所见,在源代码中,您必须实现以下接口:
/**
* 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。
objectAnimator
来实现它。 - mmlooloo