简体   繁体   English

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

[英]Android: TextView with multiples colors and fonts

Ok, so I gather that this should be a fairly straightforward process. 好的,所以我认为这应该是一个相当简单的过程。

I've read the following questions: 我读过以下问题:

The advice seems to be pretty similar across all of these questions and answers. 在所有这些问题和答案中,建议似乎非常相似。 I'm trying to avoid the HTML techniques and use SpannableString and SpannableStringBuilder instead. 我试图避免使用HTML技术,而是使用SpannableStringSpannableStringBuilder Ultimately, I'd like to be able to use multiple different typefaces in a single TextView, but for now, I'd just like to figure out how to get multiple colors working. 最后,我希望能够在单个TextView中使用多种不同的字体,但就目前而言,我只想弄清楚如何使多种颜色起作用。

I'm trying to implement those techniques in this way: 我正试图以这种方式实现这些技术:

// 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);

The net result is that the TextView contains the correct text contents. 最终结果是TextView包含正确的文本内容。 The TextView is the correct typeface. TextView是正确的字体。 But the entire contents of the TextView are my @color/light_gray color. TextView的全部内容都是我的@color/light_gray颜色。 I'm not sure why, because in the XML layout file, I had specified my @color/dark_gray color (which I expect to be overridden by setting the text with a Spannable ). 我不知道为什么,因为在XML布局文件, 指定我的@color/dark_gray颜色(我希望通过设置与文本覆盖Spannable )。 Even if I change both ForegroundColorSpan objects to use R.color.dark_gray , the TextView still comes out light gray. 即使我将两个ForegroundColorSpan对象都更改为使用R.color.dark_grayTextView仍然会显示为浅灰色。 I don't see anywhere else in my code where I'm setting the color of the text, so I'm really at a loss. 在我的代码中我没有看到其他地方我正在设置文本的颜色,所以我真的很茫然。

I'm running this on an LG Optimus G Pro, which is running 4.4.2. 我在LG Optimus G Pro上运行,运行4.4.2。 I have another TextView where I need to get multiple colors and font working and even underline some parts of the text, so this is a pretty big deal for me. 我有另一个TextView ,我需要获得多种颜色和字体工作,甚至下划线文本的某些部分,所以这对我来说是一个非常大的问题。 Where am I going wrong? 我哪里错了?

use getResource().getColor(R.color.light_gray) to retrieve the color you are passing to the ForegroundColorSpan . 使用getResource().getColor(R.color.light_gray)来检索传递给ForegroundColorSpan的颜色。 I doubt it is retrieving it internally for you. 我怀疑它是否在内部为您检索。 You probably need to instantiate a new ForegroundColorSpan at every iteration. 您可能需要在每次迭代时实例化一个新的ForegroundColorSpan It is not possible to reuse it 无法重复使用它

You may Use SpannableStringBuilder because it implements from spannable and CharSequence, also you may do anything with following 您可以使用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);

I have add colors.xml in values 我在值中添加了colors.xml

<color name="black">#000000</color>
<color name="grey">#DCDCDC</color>
<color name="white">#FFFFFF</color>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM