我有这个字符串 String thestring="<p>Abcd® X (CSX) Open Cell</p>"
,
我已经使用 Html.from 跳过像这样打印的标签:
Spanned spst = Html.fromHtml(thestring);
我也希望 ® 是上标,所以我使用了下面的代码,
SpannableStringBuilder cs = new SpannableStringBuilder(spst);
cs.setSpan(new SuperscriptSpan(),thestring.indexOf("®") ,thestring.indexOf("®")+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new RelativeSizeSpan(0.75f), thestring.indexOf("®"),thestring.indexOf("®")+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
content.setText(cs, TextView.BufferType.SPANNABLE);
但这不是制作 ® 上标, Spanned 对象和字符串的索引不同,我怎样才能在 Spanned 字符串中获取 ® 的索引?
最佳答案
将 Spanned
转换为 String
,这将使您“干净”,从 HTML 字符串中剥离
String strippedFromHTMLString = spst.toString();
然后在 setSpan
中使用它代替原来的 thestring
int indexOfR = strippedFromHTMLString.indexOf("®");
SpannableStringBuilder cs = new SpannableStringBuilder(spst);
cs.setSpan(new SuperscriptSpan(), indexOfR, indexOfR+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new RelativeSizeSpan(0.75f), indexOfR, indexOfR+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
content.setText(cs, TextView.BufferType.SPANNABLE);
关于java - 如何获取跨对象中字符的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45060331/