Can I underline text in an android layout?

如何在Android布局xml文件中定义带下划线的文本?


It can be achieved if you are using a string resource xml file, which supports HTML tags like <b></b> , <i></i> and <u></u> .

<resources>
    <string name="your_string_here">This is an <u>underline</u>.</string>
</resources>

If you want to underline something from code use:

TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);

你可以试试

textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);

The "accepted" answer above does NOT work (when you try to use the string like textView.setText(Html.fromHtml(String.format(getString(...), ...))) .

As stated in the documentations you must escape (html entity encoded) opening bracket of the inner tags with &lt; , eg result should look like:

<resource>
    <string name="your_string_here">This is an &lt;u>underline&lt;/u>.</string>
</resources>

Then in your code you can set the text with:

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(String.format(getString(R.string.my_string), ...)));
链接地址: http://www.djcxy.com/p/84748.html

上一篇: Android布局文件夹是否可以包含子文件夹?

下一篇: 我可以在android布局中加下文字吗?