Can underline words in TextView text

2019-03-12 01:54发布

Is there possibility in android to provide TextView some text in Java code with setText(text) function with basic tags like and to make marked words underlined ?

5条回答
叛逆
2楼-- · 2019-03-12 02:32

You can use UnderlineSpan from SpannableString class:

SpannableString content = new SpannableString(<your text>);
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

Then just use textView.setText(content);

查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-12 02:38

Yes, you can, use the Html.fromhtml() method:

textView.setText(Html.fromHtml("this is <u>underlined</u> text"));
查看更多
走好不送
4楼-- · 2019-03-12 02:43

Define a string as:

<resources>
    <string name="your_string">This is an <u>underline</u> text demo for TextView.</string>
</resources>
查看更多
Viruses.
5楼-- · 2019-03-12 02:45
tobeunderlined= <u>some text here which is to be underlined</u> 

textView.setText(Html.fromHtml("some string"+tobeunderlined+"somestring"));
查看更多
虎瘦雄心在
6楼-- · 2019-03-12 02:54

You can use pretty much all HTML tags on TextView. See an example here.

查看更多
登录 后发表回答