在 Android 中的 TextView 下方绘制下划线 [英] To draw an Underline below the TextView in Android

查看:26
本文介绍了在 Android 中的 TextView 下方绘制下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 TextView 下方画下划线.我搜索了一些内容,但没有找到任何成果.

谁能帮帮我?

解决方案

TextView 中文字下划线的三种方式.

  1. SpannableString

  2. setPaintFlags(); 的 TextViewp>

  3. Html.fromHtml();

让我向您解释所有方法:

第一种方法

对于 TextView 中的文本,您必须使用 SpannableString

String udata="下划线文本";SpannableString 内容 = 新的 SpannableString(udata);content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);mTextView.setText(内容);

第二种方法

你可以使用TextView的setPaintFlags方法来下划线TextView 的文本.

例如.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);mTextView.setText("此文本将带下划线");

如果您想穿透文本,可以参考 Paint 类的常量.

第三种方法

利用Html.fromHtml(htmlString);

String htmlString="<u>此文本将带下划线</u>";mTextView.setText(Html.fromHtml(htmlString));

txtView.setText(Html.fromHtml("<u>下划线</u>文本"));

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 here?

解决方案

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"));

这篇关于在 Android 中的 TextView 下方绘制下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆