SpannableStringBuilder to create String with multiple fonts/text sizes etc Example?

后端 未结 9 1545
眼角桃花
眼角桃花 2020-11-28 03:00

I need to create a String placed in a TextView that will display a string like this:

First Part Not Bold BOLD rest not bold

9条回答
  •  余生分开走
    2020-11-28 03:59

    We can also use SpannableStringBuilder with TextAppearanceSpan to accomplish that. Follow the below steps to implement like that.

    1. Create a style in styles.xml.

    1. Use the below code.
    SpannableStringBuilder builder = new SpannableStringBuilder("First Part Not Bold BOLD rest not bold");
    builder.setSpan(new TextAppearanceSpan(this, R.style.BoldStyle), 20, 24, 0);
    ((TextView)findViewById(R.id.tv7)).setText(builder);
    

    That's it. Hope it'll help someone.

提交回复
热议问题