How to set underline text on textview?

后端 未结 12 1182
慢半拍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 17:52

    Android supports string formatting using HTML tags in the strings xml. So the following would work:

    This text has an underscore
    

    Then you just use it as you normally would. You can see more in the documentation here

    Update:

    To further expand on the answer, the documentation says:

    ... the format(String, Object...) and getString(int, Object...) methods strip all the style information from the string. . The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place

    For example, the resulting string from the following code snippet will not show up as containing underscored elements:

    This %1$s has an underscore
    String result = getString(R.string.html_bold_test, "text")
    

    However the result from the following snippet would contain underscores:

    This <u> %1$s </u> has an underscore
    String result = Html.fromHtml(getString(R.string.html_bold_test, "text"))
    

提交回复
热议问题