指定“删除线"在 TextView 文本的一部分上 [英] Specifying "strikethrough" on a section of TextView text

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

问题描述

我有一个来自网络服务的文本块,根据我预定义的一些标签,我想在将文本设置为我的 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 不支持strike"标签,所以不能以同样的方式完成.我已经看到使用 Spannable 设置文本部分样式的示例,但它需要位置数字.所以,我想我可以遍历文本,搜索 - (代表罢工的标签),然后搜索下一个,跨越中间的文本,并重复所有这样的字符串.它最终将是 10 行循环代码,而其他代码只有 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 你可以使用paint flags来穿透

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

这篇关于指定“删除线"在 TextView 文本的一部分上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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