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

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

    Or just like this in Kotlin:

    val tv = findViewById(R.id.textViewOne) as TextView
    tv.setTypeface(null, Typeface.BOLD_ITALIC)
    // OR
    tv.setTypeface(null, Typeface.BOLD or Typeface.ITALIC)
    // OR
    tv.setTypeface(null, Typeface.BOLD)
    // OR
    tv.setTypeface(null, Typeface.ITALIC)
    // AND
    tv.paintFlags = tv.paintFlags or Paint.UNDERLINE_TEXT_FLAG
    

    Or in Java:

    TextView tv = (TextView)findViewById(R.id.textViewOne);
    tv.setTypeface(null, Typeface.BOLD_ITALIC);
    // OR
    tv.setTypeface(null, Typeface.BOLD|Typeface.ITALIC);
    // OR
    tv.setTypeface(null, Typeface.BOLD);
    // OR
    tv.setTypeface(null, Typeface.ITALIC);
    // AND
    tv.setPaintFlags(tv.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG);
    

    Keep it simple and in one line :)

提交回复
热议问题