How do I change color of a certain part of my textview?

半腔热情 提交于 2020-01-06 01:32:11

问题


I am using 2 parts in a textview, 1st part is date another is name and email. They are both referenced in the same textview. I would like to change the color of the date to give it a different visual it from name and email. is it possible to do this without actually adding a whole new textview for name and email? Here's my code so far:

String nameandemail;
holder.mytext.setText(String.valueOf(dateFormat.format(new Date(msg.getDate())) + " " + nameandemail + ": "));

How do I make it such that I can set the color of date with holder.mytext.setTextColor(Color.white) and for the nameandemail string something like green?

Thanks!


回答1:


you could define a String in your strings.xml file

 <string name="test2">&lt;font color=\'#FFFFFF\'>%1$s&lt;/font>  -- &lt;font color=\'#00FF00\'>%2$s&lt;/font></string>

and then programmatically

TextView tv = (TextView) findViewById(R.id.test);
 tv.setText(Html.fromHtml(getString(R.string.test2, String.valueOf(dateFormat.format(new Date(msg.getDate())), nameandemail)));



回答2:


You can Use spans.

final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");

// Set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158)); 

// Make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); 

// Set the text color for first 6 characters
sb.setSpan(fcs, 0, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

// make them also bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

textView.setText(sb);

You can also use html like below

  myTextView.setText(Html.fromHtml(text + "<font color=white>" + some_text + "</font><br><br>"
        + some_text));



回答3:


My recommendation would be to use Spannable.

Here is a short utils method I wrapped up for you to use. You simply need to pass your TextView, your full text and the single part to be re-colored from the full text.

You can place this method to a Utils class and call it whenever you want, or keep it in a single Activity or Fragment(or wherever else) if you use it in a single class:

public static void colorText(TextView view, final String fullText, final String whiteText) {
    if (fullText.length() < whiteText.length()) {
        throw new IllegalArgumentException("'fullText' parameter should be longer than 'whiteText' parameter ");
    }
    int start = fullText.indexOf(whiteText);
    if (start == -1) {
        return;
    }
    int end = start + whiteText.length();

    SpannableStringBuilder finalSpan = new SpannableStringBuilder(fullText);
//    finalSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(view.getContext(),R.color.your_own_color_code)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    finalSpan.setSpan(new ForegroundColorSpan(Color.WHITE), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    view.setText(finalSpan);
}


来源:https://stackoverflow.com/questions/33246735/how-do-i-change-color-of-a-certain-part-of-my-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!