【问题标题】:How to display separators in Android SeekBar?如何在 Android SeekBar 中显示分隔符?
【发布时间】:2016-10-29 05:23:29
【问题描述】:

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

【问题讨论】:

标签: android seekbar


【解决方案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);
    }
}

【讨论】:

  • 这应该被接受,就像一个魅力,而且干净。
【解决方案2】:

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

更多信息见链接Seek bar with divider

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 2014-01-24
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多