要解决Android TabLayout指示符的绘制颜色不可见,并且无法添加自定义宽度的问题,您可以使用自定义的TabLayout指示符来实现。
public class CustomTabLayoutIndicator implements TabLayout.TabIndicatorInterpolator {
private final Paint paint;
private final int color;
private final int width;
public CustomTabLayoutIndicator(int color, int width) {
this.color = color;
this.width = width;
this.paint = new Paint();
this.paint.setColor(color);
this.paint.setStrokeWidth(width);
this.paint.setStyle(Paint.Style.FILL_AND_STROKE);
}
@Override
public void draw(Canvas canvas, int startX, int stopX, int startY, int stopY, int selectedPosition, float selectionOffset) {
float indicatorStartX = startX + width / 2;
float indicatorStopX = stopX - width / 2;
float indicatorY = startY + (stopY - startY) * (selectedPosition + selectionOffset);
canvas.drawLine(indicatorStartX, indicatorY, indicatorStopX, indicatorY, paint);
}
}
TabLayout tabLayout = findViewById(R.id.tab_layout);
CustomTabLayoutIndicator customTabLayoutIndicator = new CustomTabLayoutIndicator(ContextCompat.getColor(this, R.color.indicator_color), getResources().getDimensionPixelSize(R.dimen.indicator_width));
tabLayout.setSelectedTabIndicator(customTabLayoutIndicator);
请确保将颜色资源和尺寸资源替换为您自己的颜色和宽度值。
通过使用自定义的TabLayout指示符绘制类,您可以在TabLayout中绘制自定义的指示符,并设置颜色和宽度,以满足您的需求。