为什么以下代码不起作用?它适用于Toast但不适用于TextView.当我运行程序时,boldName不会显示为粗体,但当我将其设置为Toast时,它确实显示为粗体.有没有人有其他解决方案?
String boldName = "<b>" + name + "</b>";
Spanned conBold = Html.fromHtml(boldName);
chosen_contact.setText("You have chosen " + conBold + " as your contact.");
Run Code Online (Sandbox Code Playgroud)
Mic*_*ski 21
老实说,我不确定为什么TextViews按照他们的方式行事,你可以将它们全部设置为粗体,但只有当整个TextView都是粗体时,如果只有部分粗体并且有那里的其他字符串.
但是,此代码适用于您:
// 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13518 次 |
| 最近记录: |