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

前端 未结 11 617
借酒劲吻你
借酒劲吻你 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:20

    Programmatialy:

    You can do programmatically using setTypeface() method:

    Below is the code for default Typeface

    textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
    textView.setTypeface(null, Typeface.BOLD);        // for Bold only
    textView.setTypeface(null, Typeface.ITALIC);      // for Italic
    textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
    

    and if you want to set custom Typeface:

    textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      // for Normal Text
    textView.setTypeface(textView.getTypeface(), Typeface.BOLD);        // for Bold only
    textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      // for Italic
    textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic
    

    XML:

    You can set Directly in XML file in like:

    android:textStyle="normal"
    android:textStyle="normal|bold"
    android:textStyle="normal|italic"
    android:textStyle="bold"
    android:textStyle="bold|italic"
    

提交回复
热议问题