改变TextView下划线颜色

4

我将尝试更改TextView中下划线指示器的颜色。

我知道可以像这样给文本添加下划线

Spanned htmlString = Html.fromHtml("<u> <font color=\"#FF0000\"> some text </font> </u>")
someTextView.setText(htmlString);

就像这样

SpannableString content = new SpannableString(test);
content.setSpan(new UnderlineSpan(), 0, test.length(), 0);
tvTest.setText(content);

但两种解决方案都使用相同的颜色来显示文本和下划线指示器。我想知道是否可以在不使用任何背景XML和反射技巧(如此处所见)的情况下以合理的方式更改下划线指示器的颜色。 我只想要对文本进行下划线,而不是对填充、边距进行下划线。我的文本可能跨越多行,我希望每一行都有下划线。(这就是XML解决方案失败的地方)。
我的代码结果: enter image description here
1个回答

17

回答自己的问题感觉有些奇怪,但为了帮助有同样问题的人,我会这样做。在阅读一些其他帖子以在EditText上执行此操作时,我偶然发现了Layout类。它通过手动使用画布绘制下划线提供了实现所需的所有内容。

首先,我定义了自定义属性,以便在XML布局文件中轻松进行自定义。

<declare-styleable name="UnderlinedTextView" >
    <attr name="underlineWidth" format="dimension" />
    <attr name="underlineColor" format="color" />
</declare-styleable>

以及一个自定义的 TextView

public class UnderlinedTextView extends AppCompatTextView {

    private Rect lineBoundsRect;
    private Paint underlinePaint;

    public UnderlinedTextView(Context context) {
        this(context, null, 0);
    }

    public UnderlinedTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    private void init(Context context, AttributeSet attributeSet, int defStyle) {

        float density = context.getResources().getDisplayMetrics().density;

        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.UnderlinedTextView, defStyle, 0);
        int mColor = typedArray.getColor(R.styleable.UnderlinedTextView_underlineColor, 0xFFFF0000);
        float mStrokeWidth = typedArray.getDimension(R.styleable.UnderlinedTextView_underlineWidth, density * 2);
        typedArray.recycle();

        lineBoundsRect = new Rect();
        underlinePaint = new Paint();
        underlinePaint.setStyle(Paint.Style.STROKE);
        underlinePaint.setColor(mColor); //color of the underline
        underlinePaint.setStrokeWidth(mStrokeWidth);
    }

    @ColorInt
    public int getUnderLineColor() {
        return underlinePaint.getColor();
    }

    public void setUnderLineColor(@ColorInt int mColor) {
        underlinePaint.setColor(mColor);
        invalidate();
    }

    public float getUnderlineWidth() {
       underlinePaint.getStrokeWidth()
    }

    public void setUnderlineWidth(float mStrokeWidth) {
        underlinePaint.setStrokeWidth(mStrokeWidth);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int count = getLineCount();

        final Layout layout = getLayout();
        float x_start, x_stop, x_diff;
        int firstCharInLine, lastCharInLine;

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, lineBoundsRect);
            firstCharInLine = layout.getLineStart(i);
            lastCharInLine = layout.getLineEnd(i);

            x_start = layout.getPrimaryHorizontal(firstCharInLine);
            x_diff = layout.getPrimaryHorizontal(firstCharInLine + 1) - x_start;
            x_stop = layout.getPrimaryHorizontal(lastCharInLine - 1) + x_diff;

            canvas.drawLine(x_start, baseline + mStrokeWidth, x_stop, baseline + mStrokeWidth, underlinePaint);
        }

        super.onDraw(canvas);
    }
}

然后它的用法很简单

<some.package.UnderlinedTextView
    android:id="@+id/tvTest"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:gravity="center"
    android:text="This is a demo text"
    android:textSize="16sp"
    app:underlineColor="#ffc112ef"
    app:underlineWidth="3dp"/>

最终结果

  • 多行

    enter image description here
  • 单行

    enter image description here

真的是一个很好的方法,恭喜!但是在使用它时,我遇到了一个问题,行不总是以相同的方式对齐:有时它们在单词之前结束,有时在单词之后一点。我不知道这可能是填充或其他什么问题。你知道为什么会发生这种情况吗?谢谢! - dleal
1
@BojanKseneman,我如何在这个自定义视图中仅对特定文本进行下划线,而不使用Spannable或其他方法... - Reprator
@BojanKseneman 很棒的答案,只有一个问题: 如何增加下划线和文本之间的间距,因为它看起来太接近了? - Adarsh Yadav
我找到了答案 - https://dev59.com/2aTja4cB1Zd3GeqPDo4i#47461749 感谢大家的帮助。 - Adarsh Yadav
这是可行的代码,我已经测试过了。如果有人提取任何问题,请使用此代码。 - Nitya Nand Pandey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接