Android 中为 TextView 的文本添加下划线

代码与范例:

// 方案一
String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

// 方案二
mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

// 方案三
String htmlString="This text will be underlined";
mTextView.setText(Html.fromHtml(htmlString));

说明:我们可以通过 SpannableString、TextView 的 setPaintFlags(),或者 Html.fromHtml() 这三种方式为 TextView 的文本添加下划线。继承于 TextView 的控件也支持这些操作。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注