如何在Android SeekBar中显示分隔符?

时间:2016-10-29 05:23:29

标签: android seekbar

enter image description here

我在我的一个片段中添加了一个SeekBar。努力添加白线(分隔线),如上面的SeekBar所示。任何线索?我可以为此设置任何属性吗?

2 个答案:

答案 0 :(得分:1)

以下是我在基础课上所做的工作。你需要让它充满活力:

public class SegmentedSeekBar extends android.support.v7.widget.AppCompatSeekBar {

    private Paint progressPaint;

    public SegmentedSeekBar(Context context) {
        super(context);
        init();
    }

    public SegmentedSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SegmentedSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        progressPaint = new Paint();
        progressPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        progressPaint.setColor(Color.LTGRAY);
        progressPaint.setStrokeWidth(2.0f);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        int halfHeight = getHeight() / 2;

        int pos;
        float smallH = 10;

        float div = (getWidth() - getPaddingRight() - getPaddingLeft()) / (getMax());

        for (int i = 1; i < getMax(); i++) {
            pos = (int) (div * i) + getPaddingLeft();

            canvas.drawLine(
                    pos + 1.0f,
                    halfHeight - (halfHeight / 2.0f),
                    pos + 1.0f,
                    halfHeight + (halfHeight / 2.0f),
                    progressPaint);
        }

        super.onDraw(canvas);
    }
}

答案 1 :(得分:0)

完成您的要求的最佳方法是在搜索栏上添加空视图(3 View)。使用match_parent高度创建视图,在搜索栏上创建2dp宽度。

有关详细信息,请参阅链接Seek bar with divider