繁体   English   中英

如何更改活动标签指示器的颜色? (安卓工作室)

[英]How to change the color of the active tab indicator? (Android Studio)

我想知道我是否能够将活动标签指示器(那条浅蓝色线条)的颜色更改为黑色(#4A4A4A)

这是照片

如果您正在使用支持库。 将此添加到您的 tabLayout

app:tabIndicatorColor="#4A4A4A"
public class PageIndicator extends View {
    int totalNoOfDots;
    int activeDot;
    int dotSpacing;
    int horizontalSpace = 5;
    Bitmap activeDotBitmap;
    Bitmap normalDotBitmap;
    int x = 0;

    private Paint paint;

    public PageIndicator(Context context) {
        super(context);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        activeDotBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dot_active);
        normalDotBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dot_normal);
    }

    public PageIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        activeDotBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dot_active);
        normalDotBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dot_normal);

    }

    public PageIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        activeDotBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dot_active);
        normalDotBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dot_normal);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawDot(canvas);
        super.onDraw(canvas);

    }


    private void drawDot(Canvas canvas) {
        for (int i = 0; i < totalNoOfDots; i++) {
            if (i == activeDot) {
                canvas.drawBitmap(activeDotBitmap, x, 0, paint);
            } else {
                canvas.drawBitmap(normalDotBitmap, x, 0, paint);
            }
            x = x + activeDotBitmap.getWidth() + horizontalSpace + dotSpacing;
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = totalNoOfDots * (activeDotBitmap.getWidth() + horizontalSpace + getDotSpacing());
        width = resolveSize(width, widthMeasureSpec);
        int height = activeDotBitmap.getHeight();
        height = resolveSize(height, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    public void refersh() {
        x = 0;
        invalidate();
    }


    public int getTotalNoOfDots() {
        return totalNoOfDots;
    }

    public void setTotalNoOfDots(int totalNoOfDots) {
        this.totalNoOfDots = totalNoOfDots;
        x = 0;
        invalidate();
    }

    public int getActiveDot() {
        return activeDot;
    }

    public void setActiveDot(int activeDot) {
        this.activeDot = activeDot;
        x = 0;
        invalidate();
    }

    public int getDotSpacing() {
        return dotSpacing;
    }

    public void setDotSpacing(int dotSpacing) {
        this.dotSpacing = dotSpacing;
        x = 0;
        invalidate();
    }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM