如何在Android布局的xml
文件中定义*下划线的文字?
如果你使用的是字符串资源xml文件,支持像<b></b>
、<i></i>
和<u></u>
等HTML标签,就可以实现。
<resources>
<string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
如果你想在代码中加下划线,请使用。
TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);
上面的"accepted"答案并不***工作(当你试图使用字符串,如textView.setText(Html.fromHtml(String.format(getString(..), ...))
。
正如文档中所述,你必须用<
来转义(html实体编码)内部标签的开头括号,例如,结果应该是这样的。
<resource>
<string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
然后在你的代码中,你可以用以下方式设置文本。
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(String.format(getString(R.string.my_string), ...)));
Strings.xml文件内容。
<resource>
<string name="my_text">This is an <u>underline</u>.</string>
</resources>
布局xml文件应使用上述字符串资源,并具有以下文本视图的属性,如下所示。
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/my_text"
android:selectAllOnFocus="false"
android:linksClickable="false"
android:autoLink="all"
/>