How to set the font style to bold, italic and underlined in an Android TextView?

前端 未结 11 619
借酒劲吻你
借酒劲吻你 2020-12-02 04:04

I want to make a TextView\'s content bold, italic and underlined. I tried the following code and it works, but doesn\'t underline.



        
11条回答
  •  心在旅途
    2020-12-02 04:41

    This should make your TextView bold, underlined and italic at the same time.

    strings.xml

    
        Copyright
    
    

    To set this String to your TextView, do this in your main.xml

    
    
    

    or In JAVA,

    TextView textView = new TextView(this);
    textView.setText(R.string.register);
    

    Sometimes the above approach will not be helpful when you might have to use Dynamic Text. So in that case SpannableString comes into action.

    String tempString="Copyright";
    TextView text=(TextView)findViewById(R.id.text);
    SpannableString spanString = new SpannableString(tempString);
    spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
    spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
    spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
    text.setText(spanString);
    

    OUTPUT

    enter image description here

提交回复
热议问题