Is it possible to have multiple styles inside a TextView?

后端 未结 18 1848
醉梦人生
醉梦人生 2020-11-21 17:34

Is it possible to set multiple styles for different pieces of text inside a TextView?

For instance, I am setting the text as follows:

tv.setText(line         


        
18条回答
  •  再見小時候
    2020-11-21 17:52

    If you don't feel like using html, you could just create a styles.xml and use it like this:

    TextView tv = (TextView) findViewById(R.id.textview);
    SpannableString text = new SpannableString(myString);
    
    text.setSpan(new TextAppearanceSpan(getContext(), R.style.myStyle), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    text.setSpan(new TextAppearanceSpan(getContext(), R.style.myNextStyle), 6, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    tv.setText(text, TextView.BufferType.SPANNABLE);
    

提交回复
热议问题