How to set underline text on textview?

后端 未结 12 1220
慢半拍i
慢半拍i 2020-12-23 17:42

How to set underline text on textview?

I have used following code but it is not working.

tvHide.setText(Html.fromHtml("

12条回答
  •  长情又很酷
    2020-12-23 18:13

    Following are the some approaches for underlined text in android:

    1st Approach

    You can define your string in strings.xml

    Underlined text
    

    And use that string in your xml file

    
    

    Or you can use that string in your Activity/Fragment

    txtView.setText(R.string.your_string);
    

    2nd Approach

    To underline the text in TextView, you can use SpannableString

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

    3rd Approach

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

    txtView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    txtView.setText("Underlined Text");
    

    4th Approach

    Make use of Html.fromHtml(htmlString);

    String htmlString="Underlined Text";
    txtView.setText(Html.fromHtml(htmlString));
    

    Or

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

    Note:

    If you have added this line android:textAllCaps="true" in your layout, then none of the above will work. For that, you have to define your string in Caps and then any of the above approach.

提交回复
热议问题