SpannableString 第一次改变颜色,但不会在重用时改变 [英] SpannableString changes color first time, but not when reused

查看:17
本文介绍了SpannableString 第一次改变颜色,但不会在重用时改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一款游戏来帮助孩子们学习数学.我为代码编号着色,以便他们可以遵循逻辑.我还使用 textView.append() 构建了一个长字符串以放入活动中.

I'm trying to make a game to help kids learn math. I color code numbers so they can follow the logic. I also use textView.append() to build a long string to put into the activity.

因为我在指令中重复使用相同的数字/符号,我想要一种方法来轻松回收 SpannableStrings,但似乎我所做的更改第一次正确应用,但此后有时有效,有时部分有效(例如正确的尺寸/对齐方式,但颜色错误),或者根本不起作用.我得到了这一切的混合.我似乎在 Stackoverflow 或 Google 上的其他任何地方都找不到遇到类似问题的人.

Because I re-use the same numbers/symbols in instruction, I wanted a way to easily recycle the SpannableStrings, but it seems that the changes I make are applied correctly the first time, but thereafter sometimes work, sometimes partially work (e.g. right size/alignment, but wrong color), or don't work at all. I'm getting a mix of it all. I can't seem to find anyone experiencing a similar problem in Stackoverflow or anywhere else on Google.

这是我的代码(简化):

Here's my code (simplified):

    // Declare static variables
    public static Spannable PLUS = new SpannableString("+ ");
    public static Spannable MINUS = new SpannableString("- ");
    public static Spannable TIMES = new SpannableString("X ");
    public static Spannable DIVIDED_BY = new SpannableString("/ ");
    public static Spannable EQUALS = new SpannableString("= ");

    // Function to color these strings.
    public void colorStrings() {
    makeBlack(PLUS);
    makeBlack(MINUS);
    makeBlack(TIMES);
    makeBlack(DIVIDED_BY);
    makeBlack(EQUALS);
    makeBlack(QMARK);
    }

    // Function that actually sets color, size, and alignment.
    public Spannable makeBlack(Spannable s) {s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_black)), 0, s.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    s.setSpan(new AbsoluteSizeSpan((int) getResources().getDimension(R.dimen.big_text)), 0, s.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    s.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), 0, s.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return s;
    }

    // Implementing above code by setting math1 TextView to give it colors.
    // Note that this is the exact code used for the first colored string in the
    // screen capture below.
    math1.setText(XX);
    math1.append(PLUS);
    math1.append(YY);
    math1.append(PLUS);
    math1.append(ZZ);
    math1.append(EQUALS);
    math1.append(QMARK);

(请注意,我没有提供 XX、YY 或 ZZ 的代码……这有点复杂,因为我必须从 sharedpreference 设置中获取数据,但是这些遇到的问题与其他数学符号/运算符).

(Note that I didn't provide the code for XX, YY, or ZZ...it's a bit more involved, since I have to get the data from a sharedpreference setting, but these experience essentially the same problem as the other math symbols/operators).

这是生成的屏幕截图:

也许我的问题最好的例子是 +(加号).一开始,它和预期的一样是黑色的,但只有 4 个字符之后(包括空格),它的颜色是错误的(蓝色,它可能是从之前的 SpannableString 中获得的),尽管对齐和大小正确.在下一个彩色字符串中,它按预期显示(黑色、居中、大),但之后在后续的彩色"字符串(小、左对齐、绿色,如段落文本)中根本没有格式化. 使用的代码与上面完全一样(即 math1.append(PLUS);),但输出与我预期的不一致.

Perhaps the best example of my problem is the + (plus sign). At first, it's black as expected, but only 4 characters later (including spaces) it's the wrong color (blue, which it likely got from the previous SpannableString), though aligned and sized correctly. In the next colored string, it's displayed as expected (black, centered, large), but after that it's not formatted at all in the subsequent "colored" strings (small, left-aligned, green, like paragraph text). The code used looks exactly as above (i.e. math1.append(PLUS);), but the output is not consistently what I expected.

最后一条信息:在全局声明我的变量之前,我每次都使用相同的 makeBlack() 和 math1.append() 函数在本地声明它们,并且效果很好.在全局设置我的变量后,我只会得到这种奇怪的行为.

Last piece of information: Prior to globally declaring my variables, I locally declared them every time using the same makeBlack() and math1.append() functions, and it worked perfectly. I'm only getting this strange behavior after globally setting my variables.

对这里出了什么问题有什么建议吗?

Any suggestions on what's going wrong here?

推荐答案

您不能多次重复使用相同的跨度.即使跨度完全相同,您也需要为要更改的字符串的每个部分创建唯一的跨度.

You can't reuse the same span multiple times. Even if the span is exactly the same, you need to create a unique span for each section of the string you are wanting to change.

我不确定我在哪里读到的,但我可以确认答案.

I am not sure where I read this, but I can confirm the answer.

为您想要更改的每件事情创建一个独特的跨度.

Create a unique span for each thing you would like to change.

这篇关于SpannableString 第一次改变颜色,但不会在重用时改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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