To draw an Underline below the TextView in Android

后端 未结 8 877
孤街浪徒
孤街浪徒 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 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="This text will be underlined";
    mTextView.setText(Html.fromHtml(htmlString));
    

    OR

    txtView.setText(Html.fromHtml("underlined text"));
    

提交回复
热议问题