问题描述
这是加入spannable跨度的优先级会影响在一个TextView它的外观。
当我们添加两个嵌套字符样式到spannable像下面
The priority of spans that added to a spannable affects on appearance of it in a TextView.When we add two nested Character Style to a spannable like below
SpannableStringBuilder sp = new SpannableStringBuilder();
String blue = "blue";
String red = "red";
sp.append(blue);
sp.append(red);
sp.append(blue);
sp.setSpan(new ForegroundColorSpan(Color.RED), blue, 2*blue, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
sp.setSpan(new ForegroundColorSpan(Color.BLUE), 0, sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
((TextView)findViewById(R.id.text_view)).setText(sp);
添加字符样式的顺序是文本外观很重要。
Android的制作方式,通过Spanned.SPAN_PRIORITY标志来处理跨度的优先级。但是,当我们使用Html.fromHtml()的,重点是不在我们手里。所以,我可以改变Html.fromHtmel的输出范围的优先级或者我能让新spannable从做出新跨越的重点;
The order of adding character styles is important on appearance of text.Android make a way to handle the priority of spans through Spanned.SPAN_PRIORITY flag. But when we use of Html.fromHtml(), priorities are not in our hands. So can i change span priorities of output of Html.fromHtmel or can i make new spannable from that and make new span priorities;
推荐答案
我做我自己一点点的解决方法。它使用反转跨度创造了新的Spannable。
I did for myself a little workaround. It creates new Spannable with spans reversed.
final Spannable revertSpans(Spanned stext) {
Object[] spans = stext.getSpans(0, stext.length(), Object.class);
Spannable ret = Spannable.Factory.getInstance().newSpannable(stext.toString());
if (spans != null && spans.length > 0) {
for(int i = spans.length - 1; i >= 0; --i) {
ret.setSpan(spans[i], stext.getSpanStart(spans[i]), stext.getSpanEnd(spans[i]), stext.getSpanFlags(spans[i]));
}
}
return ret;
}
用法:
TextView tv = (TextView)finViewById(R.id.my_text_view);
Spanned stext = Html.fromHtml(text, null, myTagHandler);
Spannable sreverted = revertSpans(stext);
tv.setText(sreverted);
这也许是有用的。
Maybe this be usefull.
这篇关于如何在SpannableString扭转跨距优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!