how to have bold and normal text in same textview in android?

不打扰是莪最后的温柔 提交于 2019-12-05 13:09:27

问题


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");

i want output to be like this Health: good

where strhealth = good But it is coming out Health: good What is the mistake ?

I am using android studio 2.1.1


回答1:


 String txt1="Health: ";
 SpannableString txtSpannable= new SpannableString(txt1);
 StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
 txtSpannable.setSpan(boldSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

 builder.append(txtSpannable);

 String txt2="good";
 builder.append(txt2);

 textview1.lblStatus.setText(builder, TextView.BufferType.SPANNABLE);



回答2:


The easiest way is

textview1.setText(Html.fromHtml("<b>Health:</b> good"));

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.




回答3:


I am bit late to answer, but I created a method for easy use by using answer already provided here.

    private void setSpanString(String string1, String string2, TextView textView) {
    SpannableStringBuilder builder=new SpannableStringBuilder();
    SpannableString txtSpannable= new SpannableString(string1);
    StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
    txtSpannable.setSpan(boldSpan, 0, string1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.append(txtSpannable);
    builder.append(string2);
   textView.setText(builder, TextView.BufferType.SPANNABLE);
}



回答4:


I would use a string resource such as this:

<string name="health_status"><b>Health:</b> %1$s</string>

When you want to set the health status just use this code:

String ss1 = getString(R.string.health_status, strhealth);



回答5:


In kotlin, you can do this. I used this to bold characters/word within a string. For example:

item = "Philippines"

query = "Phil"

result = Philippines

    val spannable = SpannableString(item)
    val indexStart = item.indexOf(query)
    val indexEnd = indexStart + query.length
    spannable.setSpan(StyleSpan(Typeface.BOLD), indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

textView.text = spannable




回答6:



Below I have mentioned the code which one through you can create spannableString in Kotlin

val spannableStringBuilder = SpannableStringBuilder()

val boldSpan: StyleSpan = StyleSpan(Typeface.BOLD)

sp_text.setSpan(boldSpan, firstIndex, lastIndex,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

sp_text.setSpan(clickableSpan, firstIndex, lastIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            sp_text.setSpan(ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.colorPrimary)), firstIndex, lastIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)



回答7:


I think you should use 2 different textView, a label and one for the data. it's common and good practice



来源:https://stackoverflow.com/questions/37661755/how-to-have-bold-and-normal-text-in-same-textview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!