Android TabLayout 指示器宽度自定义教程
要自定义TabLayout指示器的宽度,可以通过设置自定义的TabIndicator类来实现。下面是一个示例:
- 创建一个自定义的TabIndicator类,继承自TabLayout中的TabIndicator类,并重写draw方法:
public class CustomTabIndicator extends TabLayout.TabIndicator {
private Paint mPaint;
private int mIndicatorWidth;
private int mIndicatorHeight;
public CustomTabIndicator(TabLayout tabLayout) {
super(tabLayout);
mPaint = new Paint();
mPaint.setColor(Color.RED);
mIndicatorWidth = 100;
mIndicatorHeight = 5;
}
@Override
public void draw(Canvas canvas) {
int indicatorLeft = getLeft() + (getWidth() - mIndicatorWidth) / 2;
int indicatorRight = indicatorLeft + mIndicatorWidth;
int indicatorTop = getTop() + (getHeight() - mIndicatorHeight) / 2;
int indicatorBottom = indicatorTop + mIndicatorHeight;
canvas.drawRect(indicatorLeft, indicatorTop, indicatorRight, indicatorBottom, mPaint);
}
}
- 在TabLayout中设置自定义的TabIndicator:
TabLayout tabLayout = findViewById(R.id.tab_layout);
CustomTabIndicator customTabIndicator = new CustomTabIndicator(tabLayout);
tabLayout.setSelectedTabIndicator(customTabIndicator);
这样就可以自定义TabLayout指示器的宽度了。在CustomTabIndicator类中,可以根据自己的需求设置指示器的颜色、宽度和高度等属性。
原文地址: https://www.cveoy.top/t/topic/oVjn 著作权归作者所有。请勿转载和采集!