我只想使用 <b>
对于部分文本,如 HTML
& CSS
,我下面的代码不正确。
textEdit.setText("<![CDATA[ <b>"+ "bold text" + " </b> ]]>" + " view other normal text" );
最佳答案
使用 SpannableString 将子字符串设置为粗体。
String completeString = "<your complete string here>";
String subString = "<the substring you want in bold>";
SpannableString ss = new SpannableString(completeString);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
ds.setTypeface(Typeface.DEFAULT_BOLD);
ds.setColor(<color that you want>);
}
};
if (completeString.indexOf(subString) > -1)
ss.setSpan(clickableSpan, completeString.indexOf(subString), completeString.indexOf(subString) + subString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textEdit.setText(ss, TextView.BufferType.SPANNABLE);
关于Android 对部分文本使用粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26649344/