Spannables不是在TextView中着色的文字 [英] Spannables not coloring text in TextView

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

问题描述

我想要颜色的最后2个字母的单词,然而当我申请的范围没有任何反应。

I'm trying to color the last 2 letters of a word, yet when I apply the span nothing happens.

下面是code申请跨度:

Here is the code for applying the span:

oldWordTextView.setHighlightStatus(true);
    //Span koji uzima cijeli tekst
    String word = oldWordTextView.getText().toString();
    Log.w(word, "Rijec za animiranje highlight");
    Log.w("U animiranju prefixa", "Aniamte");
    if(word != "" && word != " "){
    final SpannableStringBuilder oldSb = new SpannableStringBuilder(word);
    final SpannableStringBuilder currentSb = new SpannableStringBuilder(buffer);

    //Span za mijenjanje boje
    final ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.rgb(60, 184, 120));//Green
    final ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.rgb(251, 43, 51));//Red
    Spannable wordToSpan = new SpannableString(word);

    if(isValid){
        wordToSpan.setSpan(greenSpan, word.length() - 2, word.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        oldSb.setSpan(greenSpan, word.length() - 2, word.length() - 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        if(buffer != "" && buffer != " ") currentSb.setSpan(greenSpan, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        oldWordTextView.setText(wordToSpan, BufferType.SPANNABLE);
        //staticTextView.setText(currentSb);
        isHighlightDone = true;
    }
    else{
        oldSb.setSpan(redSpan, 0, word.length() - 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        if(buffer != "" && buffer != " ") currentSb.setSpan(redSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        oldWordTextView.setText(oldSb);
        staticTextView.setText(currentSb);
    }
    }

在这里是因为它的定制我的TextView类: http://pastebin.com/4qxL3m82

谢谢!

编辑:我已经测试过,并由此推断,问题出在我的自定义的TextView类。我试图在应用的OnDraw和onLayout方法的跨度,但TextView中保持不变。任何想法?

I have tested and deduced that the problem lies in my custom textview class. I have tried applying the span in onDraw and onLayout methods, but the textView remains unchanged. Any ideas?

推荐答案

为了显示SpannableString,你还需要传递的标志是这样的:

In order to display a SpannableString, you need to pass also the flag like this:

textView.setText(spannableString, TextView.BufferType.SPANNABLE);

我preFER亲手打造的实用功能上色给定的字符串的某些部分。例如,假设您想要的功能突出一个单一的字符,并以不同的颜色着色等。你可以实现这样的:

I prefer personally to create utility functions to color some parts of the given String. For example, suppose you want a function for highlighting one single character and coloring the others in a different color. You could implement something like:

// this function colors the k-th character of String s in blue and the rest in red, and returns the SpannableString
public static SpannableString highlight(String s, int k) {
        SpannableString ss = new SpannableString(s);
        ss.setSpan(new ForegroundColorSpan(Color.BLUE), 0, k, 0);
        ss.setSpan(new ForegroundColorSpan(Color.RED), k, k + 1, 0);
        ss.setSpan(new ForegroundColorSpan(Color.BLUE), k + 1, s.length(), 0);
        return ss;
    }

修改:好吧,我看到的问题是,你的方法 applyLetterSpacing 是根本无视旧的跨度。因此,持久性有机污染物在我脑海的第一个解决方法是调用时的setText 和运用他们打电话给 applyLetterSpacing 。你可以看到这里的想法 http://pastebin.com/p4Hig8Pj 但正如我在我的评论,使用说在的OnDraw 自定义着色器和 onLayout 使着色效果完全出乎意料。希望它能帮助。

EDIT: well, I saw that the problem was that your method applyLetterSpacing was simply ignoring the old spans. So, the first solution that pops in my mind is to save the spans when calling setText and applying them when calling to applyLetterSpacing. You can see the idea here http://pastebin.com/p4Hig8Pj although as I said in my comment, the use of customized shader in onDraw and onLayout makes the coloring effect totally unexpected. Hope it helped.

这篇关于Spannables不是在TextView中着色的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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