Why doesn't my text show up with style when using SpannableStringBuilder?

后端 未结 4 1564
清酒与你
清酒与你 2020-12-10 07:16

I have a problem with a SpannableString object.

Below\'s a short example:

SpannableString spanstr = new SpannableString(\"Bold please!\"         


        
相关标签:
4条回答
  • 2020-12-10 07:28

    use SpannableStringBuilder instance itself.

    contentView.setText(sb);
    

    output with your code:

    enter image description here

    0 讨论(0)
  • 2020-12-10 07:33

    Use the spannable string builder for setting as text in textview :

    contentView.setText(sb);
    

    or else you can do like this :

    SpannableStringBuilder spanstr = new SpannableStringBuilder("Bold please!");
    spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spanstr.append("\n");
    spanstr.append("The first line is bold. This one isn't.");
    contentView.setText(spanstr);
    
    0 讨论(0)
  • 2020-12-10 07:35

    Try the below. You need to set the spannable string to the textview. So set the spannable string to your text as below

    String s= "The first line is bold. This one isn't";
    String title="Bold Please!";  
    TextView tv = (TextView) findViewById(R.id.some_id);
    tv.setText("");
    SpannableString ss1=  new SpannableString(title);
    ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, ss1.length(), 0);
    tv.append(ss1);
    tv.append("\n");
    tv.append(s);
    

    I tried the above and you can see the resulting snapshot below.

    enter image description here

    0 讨论(0)
  • 2020-12-10 07:38

    If you use custom font in your device. There is such a silly bug i think. Please change your custom font to default in your device and try again.

    0 讨论(0)
提交回复
热议问题