改变android下划线的颜色
我正在开发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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有记录的方法来设置下划线颜色。但是,有一个未记录的
TextPaint.setUnderline(int, float)
方法允许您提供下划线颜色和粗细: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:我自己还没有尝试过,所以这更多的是一个想法而不是解决方案,但可能值得尝试。
UnderlineSpan
类具有方法updateDrawState
,该方法将TextPaint
作为参数。反过来,TextPain 可以有字段public int linkColor
。因此,对于您来说,
TextPaint
和UnderlineSpan
的 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 methodupdateDrawState
, which takesTextPaint
as a parameter. In turn, TextPain can has fieldpublic int linkColor
.So for you it would be something like
Reference for both
TextPaint
andUnderlineSpan
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.在 TextPaint 中,有一个字段“underlineColor”和方法“setUnderlineText”,用于指示并可用于更改下划线颜色。但是,它们是“@hide”字段和方法,要使用它们,您必须使用反射,如下所示:
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:
ds is your TextPaint Object.
遇到这种情况真是晚了。这是另一种方法,它将多个跨度设置为相同的可跨越内容:
Really late to encounter this scenrio. Here's another way, It would be to set multiple spans to the same spannable content: