可扩展文本对于TextView而言太宽 [英] Spannable text is too wide for TextView

查看:55
本文介绍了可扩展文本对于TextView而言太宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字体为sans-serif-lightTextView.要在其中链接一些单词,我将文本设置为Spannable,如下所示:

I have a TextView with the font sans-serif-light. To link some words inside, I set a text as a Spannable like this:

final Spannable text = new SpannableString(textView.getText());
final Matcher matcher = PATTERN_TAG.matcher(text);
while (matcher.find()) {
    text.setSpan(span, startIndex, endIndex, 0);
}
textView.setText(text);
textView.setMovementMethod(LinkMovementMethod.getInstance());

我应用的Span将单词的字体设置为sans-serif-medium,如下所示:

The Span that I apply sets the word's font to sans-serif-medium like this:

public abstract class ClickableTagSpan extends ClickableSpan {

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
    }
}

问题在于,由于中等字体的宽度大于原先设置的浅色字体,某些行中的文本对于TextView而言变得太宽. TextView只是截去右边的文本.如何获得TextView以尊重文本的正确宽度?

The problem is that the text becomes too wide for the TextView in some rows since the width of the medium font is larger than the light font that was orginally set. The TextView simply cuts off the text on the right edge. How can I get the TextView to respect the correct width of the text?

推荐答案

通过设置其他TypefaceSpan而不是修改ClickableSpan来解决该问题.

Solved it by setting an additional TypefaceSpan, rather than modifying the ClickableSpan.

text.setSpan(clickableSpan, startIndex, endIndex, 0);
text.setSpan(new TypefaceSpan("sans-serif-medium"), startIndex, endIndex, 0);

这使TextView正确地包装文本.

这篇关于可扩展文本对于TextView而言太宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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