Is it possible to have multiple styles inside a TextView?

后端 未结 18 2087
醉梦人生
醉梦人生 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 18:14

    As Jon said, for me this is the best solution and you dont need to set any text at runtime, only use this custom class HtmlTextView

    public class HtmlTextView extends TextView {
    
      public HtmlTextView(Context context) {
          super(context);
      }
    
      public HtmlTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
    
      public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr) 
      {
          super(context, attrs, defStyleAttr);
      }
    
      @TargetApi(21)
      public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
      }
    
      @Override
      public void setText(CharSequence s,BufferType b){
          super.setText(Html.fromHtml(s.toString()),b);
      }
    
    }
    

    and thats it, now only put it in your XML

    
    

    with your Html String

    
    Author: Mr Donuthead
    Contact: me@donut.com
    Donuts for life ]]>

提交回复
热议问题