Android: 具有多种颜色和字体的 TextView

Android: TextView with multiples colors and fonts

好的,所以我认为这应该是一个相当简单的过程。

我已阅读以下问题:

所有这些问题和答案的建议似乎都非常相似。我试图避免使用 HTML 技术,而是使用 SpannableStringSpannableStringBuilder。最终,我希望能够在单个 TextView 中使用多种不同的字体,但现在,我只想弄清楚如何让多种颜色工作。

我正在尝试以这种方式实施这些技术:

// Get a typeface for my custom font
String regularFontPath          = "fonts/Abel-Regular.ttf";
Typeface regularTf              = Typeface.createFromAsset(getActivity().getAssets(), regularFontPath);

// Set the label's typeface (this part is working)
mItemCodesLabel.setTypeface(regularTf);

// Create a spannable builder to build up my
// TextView's content from data
SpannableStringBuilder builder  = new SpannableStringBuilder();

// These colors are defined and working well in other parts of my app
ForegroundColorSpan ltGraySpan  = new ForegroundColorSpan(R.color.light_gray);
ForegroundColorSpan dkGraySpan  = new ForegroundColorSpan(R.color.dark_gray);

// mCodesList has good data and the actual data output from this
// loop is correct. Just the styling is wrong
for (int i = 0; i < mCodesList.size(); i = i + 1) {
    ParseObject code            = mCodesList.get(i);
    String value                = code.getString("value") + " | ";
    if (i > 0) {
        // I want new codes to be on a new line (this works)
        value                   = "\n" + value;
    }
    SpannableString valueSpan   = new SpannableString(value);
    valueSpan.setSpan(ltGraySpan, 0, value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.append(valueSpan);

    String loc                  = code.getString("location");
    SpannableString locSpan     = new SpannableString(loc);
    locSpan.setSpan(dkGraySpan, 0, loc.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.append(locSpan);
}
mItemCodesLabel.setText(builder);

最终结果是 TextView 包含正确的文本内容。 TextView 是正确的字体。但是TextView的全部内容都是我的@color/light_gray颜色。我不确定为什么,因为在 XML 布局文件中,我 已经 指定了我的 @color/dark_gray 颜色(我希望通过设置文本来覆盖它Spannable)。即使我将两个 ForegroundColorSpan 对象都更改为使用 R.color.dark_grayTextView 仍然是浅灰色。我在我的代码中没有看到其他任何设置文本颜色的地方,所以我真的很茫然。

我 运行 使用的是 LG Optimus G Pro,它是 运行 4.4.2。我有另一个 TextView,我需要在其中使用多种颜色和字体,甚至在文本的某些部分下划线,所以这对我来说是一件大事。我哪里错了?

使用 getResource().getColor(R.color.light_gray) 检索您传递给 ForegroundColorSpan 的颜色。我怀疑它是在内部为您检索它。您可能需要在每次迭代时实例化一个新的 ForegroundColorSpan 。无法重复使用它

您可以使用 SpannableStringBuilder,因为它是从 spannable 和 CharSequence 实现的,您也可以使用以下方法做任何事情

TextView txtTest = (TextView) findViewById(R.id.txt);
String text = "This is an example";    
final SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(new TypefaceSpan("monospace"), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new TypefaceSpan("serif"), 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.white)), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.grey)), 6, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtTest.setText(str);

我在值

中添加了 colors.xml
<color name="black">#000000</color>
<color name="grey">#DCDCDC</color>
<color name="white">#FFFFFF</color>