Java 在 textview 中将字符串的一部分设为粗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20850822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:46:52  来源:igfitidea点击:

Making part of a string bold in textview

javaandroid

提问by user3131263

Why is the following code not working? It works in a Toast but not in a TextView. boldName doesn't show up as bold when I run my program but it does show up as bold when I set it to a Toast. Does anyone have any other solutions?

为什么下面的代码不起作用?它适用于 Toast 但不适用于 TextView。当我运行我的程序时,boldName 不会显示为粗体,但是当我将它设置为 Toast 时它会显示为粗体。有没有人有其他解决方案?

String boldName = "<b>" + name + "</b>";
Spanned conBold = Html.fromHtml(boldName);
chosen_contact.setText("You have chosen " + conBold + " as your contact.");

采纳答案by Michael Yaworski

I'm honestly not sure why exactly TextViews act the way they do where you can set it all bold as you are doing, but only if they entire TextView is bold, yet you can't if only part of it is bold and there are other Strings in there.

老实说,我不确定为什么 TextViews 会按照它们的方式行事,您可以将其全部设置为粗体,但前提是它们整个 TextView 都是粗体的,但如果只有一部分是粗体的并且有其他字符串在那里。

However, this code will work for you:

但是,此代码对您有用:

// a SpannableStringBuilder containing text to display
SpannableStringBuilder sb = new SpannableStringBuilder("You have chosen " + name + " as your contact.");

// create a bold StyleSpan to be used on the SpannableStringBuilder
StyleSpan b = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold

// set only the name part of the SpannableStringBuilder to be bold --> 16, 16 + name.length()
sb.setSpan(b, 16, 16 + name.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold

chosen_contact.setText(sb); // set the TextView to be the SpannableStringBuilder

回答by Abdul Rahman

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>