gpt4 book ai didi

android - 改变TextView的下划线颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:39:30 24 4
gpt4 key购买 nike

我正在尝试更改 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 和反射 hack 的情况下以理智的方式更改下划线指示器的颜色,如此处所示

我只想给文本加下划线,而不是填充、边距。我的文本也可以跨越多行,我希望每一行都带有下划线。 (这是 XML 解决方案失败的地方)。

我的代码的结果

enter image description here

最佳答案

回答我自己的问题感觉很奇怪,但为了有同样问题的人,我会的。我偶然发现了Layout在阅读其他关于 EditText 的文章时上课。它提供了通过使用 Canvas 手动绘制下划线来实现这一目标所需的一切。

首先我定义了自定义属性以便在 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

关于android - 改变TextView的下划线颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30716673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com