settext 下划线_更改TextView的下划线颜色

I am trying to change the color of the underline indicator in a TextView.

I know I can underline text like this

Spanned htmlString = Html.fromHtml(" some text ")

someTextView.setText(htmlString);

And like this

SpannableString content = new SpannableString(test);

content.setSpan(new UnderlineSpan(), 0, test.length(), 0);

tvTest.setText(content);

But both solutions use the same color for the text and the underline indicator. I want to know if I can change the color of the underline indicator in a sane way without any background XML and reflection hacks as seen here

I want to underline only the text, not the paddings, margins. My text can span over multiple lines too, I want every line to be underlined. (This is where the XML solution fails).

Result of my code

解决方案

It feels weird answering my own question, but for the sake of anyone having the same problem, I will. I have stumbled upon Layout class when reading some other posts for doing this on EditText. It provides everything you need to make this happen by manually drawing underline with canvas.

First I defined custom attributes for an easy customization in XML layout files

And a custom TextView class

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);

}

}

Then it's usage is simple

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"/>

Final result

Multi line

Single line

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值