0

这是我的代码。我想部分更改字符串内的文本格式。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    String stuff[] = {

"this has to be  the bold part   this has to be normal part",
"this part of text has to be green      this part of text has to be blue",
"this is number 3 bold",

}

4

2 回答 2

2

您可以使用 HTML 格式:

textView.setText(Html.fromHtml("<b>test</b> test"));

于 2013-01-14T11:26:21.600 回答
0

你必须SpannableStringBuilder像下面这样使用:

    String finalText = stuff[0]+ " " + stuff[1] + " " + stuff[2];
    int first = stuff[0].length() + 1;
    int second = stuff[1].length() + 1;
    int end = finalText.length();

    SpannableStringBuilder stringBuilder = new SpannableStringBuilder(finalText);
    stringBuilder.setSpan(new CustomTypefaceSpan("", normalFont), 0, first, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    stringBuilder.setSpan(new CustomTypefaceSpan("", boldFont), first, first + second, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    stringBuilder.setSpan(new CustomTypefaceSpan("", normalFont), first + second, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

    textView.setText(stringBuilder);
于 2019-05-25T05:26:19.497 回答