指定"删除线"在TextView中的一段文字 [英] Specifying "strikethrough" on a section of TextView text

查看:305
本文介绍了指定"删除线"在TextView中的一段文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文字从一个web服务未来块,并根据一些标记,我有predefined,我想将其设置为我的TextView之前样式的文本。对于粗体,斜体和下划线,我能够用的replaceAll命令很容易做到这一点:

I have a block of text coming from a webservice, and depending on some tags which I have predefined, I want to style the text before setting it to my TextView. For bold, italics, and underline, I was able to do this easily with the replaceAll command:

PageText = PageText.replaceAll("\\*([a-zA-Z0-9]+)\\*", "<b>$1</b>");
        PageText = PageText.replaceAll("=([a-zA-Z0-9]+)=", "<i>$1</i>");            
        PageText = PageText.replaceAll("_([a-zA-Z0-9]+)_", "<u>$1</u>");
txtPage.setText(Html.fromHtml(PageText), TextView.BufferType.SPANNABLE);

因此​​,要大胆的一个字,围绕着它带*号的,为斜体,请用_

So, to bold a word, surround it with *'s, for italics, surround with _.

不过,对于删除线,Html.fromHtml不支持罢工的标签,所以它不能做同样的方式。我已经看到了使用Spannable设置文本的一个部分造型的例子,但它需要的位置编号。所以,我想我可以遍历文本,搜索 - (标签重新present罢工),然后寻找下一个,之间跨越的文本,并重复所有这样的字符串。它最终会被10线循环code,而不是1为他人着想,所以我不知道是否有一个更优雅的解决方案在那里。

But, for strikethrough, Html.fromHtml does not support the "strike" tag, so it can't be done this same way. I've seen examples of using Spannable to set the styling on one section of text, but it requires positional numbers. So, I guess I could loop through the text, searching for - (the tag to represent the strike), then searching for the next one, spanning the text in between, and repeating for all such strings. It will end up being 10 lines of looping code as opposed to 1 for the others, so I'm wondering if there is a more elegant solution out there.

推荐答案

如果它仅仅是TextView的,你可以通过使用油漆标志罢工

If it is just TextView you can strike through using paint flags

TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

这篇关于指定&QUOT;删除线&QUOT;在TextView中的一段文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆