3

我想为文本视图文本添加下划线并将下划线的颜色更改为蓝色。我已经这样做了,但是我的代码正在更改文本视图的颜色以及下划线。我只想更改下划线的颜色。我们应该怎么做。

TextView tv = (TextView) findViewById(R.id.tv);
        SpannableString content = new SpannableString(tv.getText().toString());
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
        content.setSpan(new ForegroundColorSpan(Color.BLUE), 0, content.length(), 0);
        tv.setText(content);
4

3 回答 3

2

你去吧,直接来自https://android.googlesource.com/platform/development/+/froyo%5E/samples/NotePad/src/com/example/android/notepad/NoteEditor.java

 public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x800000FF);// line color
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();
        Rect r = mRect;
        Paint paint = mPaint;

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, r);

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        }

        super.onDraw(canvas);
    }
}

现在只需在您的 xml 中正常使用它。我希望你知道你将使用<path_to_LinedEditText.LinedEditText>标签而不是<EditText>如果你想在 xml 中使用它。

于 2013-10-08T10:33:36.557 回答
2

这应该可以帮助你

paint.setARGB(125,125,125,125);
paint.setflag(Paint.UNDERLINE_TEXT_FLAG);
textView.setPaintFlags( paint.getFlags());
textView.setText("UnderLined Text");
于 2013-10-08T10:47:10.660 回答
0

为此,您必须创建自定义控件:

像这样的edittext

于 2013-10-08T10:33:21.450 回答