To draw an Underline below the TextView in Android

后端 未结 8 861
孤街浪徒
孤街浪徒 2020-11-29 16:39

I want to draw the underline below my TextView. I have searched a few content but couldn\'t find out anything fruitful.

Can anyone please help me out he

相关标签:
8条回答
  • 2020-11-29 16:58

    Its works for me.

    tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    
    0 讨论(0)
  • 2020-11-29 17:03

    There are three ways of underling the text in TextView.

    1. SpannableString

    2. setPaintFlags(); of TextView

    3. Html.fromHtml();

    Let me explain you all approaches :

    1st Approach

    For underling the text in TextView you have to use SpannableString

    String udata="Underlined Text";
    SpannableString content = new SpannableString(udata);
    content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
    mTextView.setText(content);
    

    2nd Approach

    You can make use of setPaintFlags method of TextView to underline the text of TextView.

    For eg.

    mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    mTextView.setText("This text will be underlined");
    

    You can refer constants of Paint class if you want to strike thru the text.

    3rd Approach

    Make use of Html.fromHtml(htmlString);

    String htmlString="<u>This text will be underlined</u>";
    mTextView.setText(Html.fromHtml(htmlString));
    

    OR

    txtView.setText(Html.fromHtml("<u>underlined</u> text"));
    
    0 讨论(0)
提交回复
热议问题