改变android下划线的颜色

发布于 2024-12-08 18:27:38 字数 274 浏览 0 评论 0原文

我正在开发Android应用程序。我需要在 Textview 的一些部分下划线。

SpannableString content = new SpannableString("Ack:");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tvAck.setText(content);` 

我已经使用了上面的代码。但现在我想改变下划线的颜色。谁能告诉我该怎么做。任何帮助或建议都会被接受。

I am developing the android application. I need to underline some of the Textview.

SpannableString content = new SpannableString("Ack:");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tvAck.setText(content);` 

I have used the above code for that. But now i want to change the color of the underline. Can any one tell me how to do so. Any help or suggestion is accepted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风疏影 2024-12-15 18:27:38

没有记录的方法来设置下划线颜色。但是,有一个未记录的 TextPaint.setUnderline(int, float) 方法允许您提供下划线颜色和粗细:

final class ColoredUnderlineSpan extends CharacterStyle 
                                 implements UpdateAppearance {
    private final int mColor;

    public ColoredUnderlineSpan(final int color) {
        mColor = color;
    }

    @Override
    public void updateDrawState(final TextPaint tp) {
        try {
            final Method method = TextPaint.class.getMethod("setUnderlineText",
                                                            Integer.TYPE,
                                                            Float.TYPE);
            method.invoke(tp, mColor, 1.0f);
        } catch (final Exception e) {
            tp.setUnderlineText(true);
        }
    }
}

There is no documented method to set the underline color. However, there is an undocumented TextPaint.setUnderline(int, float) method which allows you do provide the underline color and thickness:

final class ColoredUnderlineSpan extends CharacterStyle 
                                 implements UpdateAppearance {
    private final int mColor;

    public ColoredUnderlineSpan(final int color) {
        mColor = color;
    }

    @Override
    public void updateDrawState(final TextPaint tp) {
        try {
            final Method method = TextPaint.class.getMethod("setUnderlineText",
                                                            Integer.TYPE,
                                                            Float.TYPE);
            method.invoke(tp, mColor, 1.0f);
        } catch (final Exception e) {
            tp.setUnderlineText(true);
        }
    }
}
压抑⊿情绪 2024-12-15 18:27:38

我自己还没有尝试过,所以这更多的是一个想法而不是解决方案,但可能值得尝试。 UnderlineSpan 类具有方法 updateDrawState,该方法将 TextPaint 作为参数。反过来,TextPain 可以有字段 public int linkColor

因此,对于您来说,

TextPaint tp = new TextPaint();
tp.linkColor = [your color];           //not quite sure what the format should be
UnderlineSpan us = new UnderlineSpan();
us.updateDrawState(tp);
SpannableString content = new SpannableString("Ack:");
content.setSpan(us, 0, content.length(), 0); tvAck.setText(content);

TextPaintUnderlineSpan 的 Reference 都非常差,大部分 javadoc 都丢失了(您自己判断:http://developer.android.com/reference/android/text/TextPaint.html),所以我不确定如何使用这些 尽管。

I haven't tried this myself, so this is more an idea than a solution, but probably worth trying. Class UnderlineSpan has method updateDrawState, which takes TextPaint as a parameter. In turn, TextPain can has field public int linkColor.

So for you it would be something like

TextPaint tp = new TextPaint();
tp.linkColor = [your color];           //not quite sure what the format should be
UnderlineSpan us = new UnderlineSpan();
us.updateDrawState(tp);
SpannableString content = new SpannableString("Ack:");
content.setSpan(us, 0, content.length(), 0); tvAck.setText(content);

Reference for both TextPaint and UnderlineSpan are very poor, with majority of javadoc missing altogether (judge for yourself: http://developer.android.com/reference/android/text/TextPaint.html), so I'm not certain how to use these though.

冷了相思 2024-12-15 18:27:38

在 TextPaint 中,有一个字段“underlineColor”和方法“setUnderlineText”,用于指示并可用于更改下划线颜色。但是,它们是“@hide”字段和方法,要使用它们,您必须使用反射,如下所示:

Field field = TextPaint.class.getDeclaredField("underlineColor");
field.setAccessible(true);
field.set(ds, mUnderlineColor);

ds 是您的 TextPaint 对象。

In TextPaint, there has a field 'underlineColor' and method 'setUnderlineText', indicated and can use to changed the underline color. But, they are '@hide' field and method, to use them, you must using reflecting, like this:

Field field = TextPaint.class.getDeclaredField("underlineColor");
field.setAccessible(true);
field.set(ds, mUnderlineColor);

ds is your TextPaint Object.

轻许诺言 2024-12-15 18:27:38

遇到这种情况真是晚了。这是另一种方法,它将多个跨度设置为相同的可跨越内容:

SpannableString content = new SpannableString("Ack:");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
content.setSpan(
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)),
        0,
        content.length(),
        0
);
tvAck.setText(content, TextView.BufferType.SPANNABLE);

Really late to encounter this scenrio. Here's another way, It would be to set multiple spans to the same spannable content:

SpannableString content = new SpannableString("Ack:");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
content.setSpan(
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)),
        0,
        content.length(),
        0
);
tvAck.setText(content, TextView.BufferType.SPANNABLE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文