如何在android的同一textview中具有粗体和普通文本?
问题描述:
我已经搜索了Internet并尝试了以下代码,但无法正常工作
I have searched internet and tried the following code but its not working
SpannableString ss1 = new SpannableString("Health: ");
ss1.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, ss1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview1.setText("\n"+ss1+strhealth+"\n\n");
我希望输出像这样健康:好
其中strhealth =好但它出来了健康:好有什么问题吗?
where strhealth = good But it is coming out Health: good What is the mistake ?
我正在使用android studio 2.1.1
I am using android studio 2.1.1
答
最简单的方法是
textview1.setText(Html.fromHtml("<b>Health:</b> good"));
您的代码中的错误是在此处使用字符串连接:"\ n" + ss1 + strhealth +"\ n \ n"
,因为组件被视为普通字符串,所以它会删除所有格式.
The mistake in your code is to use string concatenation here: "\n"+ss1+strhealth+"\n\n"
which strips out all formatting because the components are taken as normal strings.