我可以在 Android 布局中给文本加下划线吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2394935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I underline text in an Android layout?
提问by Janusz
How can I define underlinedtext in an Android layout xml
file?
如何在 Android 布局文件中定义带下划线的文本xml
?
回答by Anthony Forloney
It can be achieved if you are using a string resourcexml file, which supports HTML tags like <b></b>
, <i></i>
and <u></u>
.
如果您使用的是字符串资源xml 文件,它可以实现它,该文件支持 HTML 标记,如<b></b>
、<i></i>
和<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);
回答by vado
You can try with
你可以试试
textview.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
回答by Ognyan
The "accepted" answer above does NOTwork (when you try to use the string like textView.setText(Html.fromHtml(String.format(getString(...), ...)))
.
在“接受”的答案上面确实不工作(当您尝试使用像串textView.setText(Html.fromHtml(String.format(getString(...), ...)))
。
As stated in the documentationsyou must escape (html entity encoded) opening bracket of the inner tags with <
, e.g. result should look like:
如文档中所述,您必须使用 转义(html 实体编码)内部标签的左括号<
,例如结果应如下所示:
<resource>
<string name="your_string_here">This is an <u>underline</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), ...)));
回答by Devendra Anurag
Strings.xml file content:
Strings.xml 文件内容:
<resource>
<string name="my_text">This is an <u>underline</u>.</string>
</resources>
Layout xml file shold use the above string resource with below properties of textview, as shown below:
布局xml文件使用上面的字符串资源和下面的textview属性,如下图:
<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"
/>
回答by awsleiman
For Button and TextView this is the easiest way:
对于 Button 和 TextView,这是最简单的方法:
Button:
按钮:
Button button = (Button) findViewById(R.id.btton1);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Textview:
文本视图:
TextView textView = (TextView) findViewById(R.id.textview1);
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
回答by gprathour
One line solution
一条线解决方案
myTextView.setText(Html.fromHtml("<p><u>I am Underlined text</u></p>"));
myTextView.setText(Html.fromHtml("<p><u>I am Underlined text</u></p>"));
It is bit late but could be useful for someone.
这有点晚了,但可能对某人有用。
回答by Killer
In Kotlin extension functioncan be used. This can only be used from code, not xml.
在 Kotlin中可以使用扩展函数。这只能从代码中使用,而不是 xml。
fun TextView.underline() {
paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
Usage:
用法:
tv_change_number.underline()
tv_resend_otp.underline()
回答by Kirill Vashilo
check out the underscored clickable button style:
查看下划线的可点击按钮样式:
<TextView
android:id="@+id/btn_some_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_add_contact"
android:textAllCaps="false"
android:textColor="#57a0d4"
style="@style/Widget.AppCompat.Button.Borderless.Colored" />
strings.xml:
字符串.xml:
<string name="btn_add_contact"><u>Add new contact</u></string>
Result:
结果:
回答by Volodymyr
The most recent approach of drawing underlined text is described by Romain Guy on mediumwith available source code on GitHub. This sample application exposes two possible implementations:
Romain Guy 在medium上描述了绘制带下划线的文本的最新方法,并在GitHub 上提供了可用的源代码。此示例应用程序公开了两种可能的实现:
- A Path-based implementation that requires API level 19
- A Region-based implementation that requires API level 1
- 需要 API 级别 19 的基于路径的实现
- 需要 API 级别 1 的基于区域的实现
回答by jk7
A cleaner way instead of thetextView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
method is to use
textView.getPaint().setUnderlineText(true);
一种更清洁的方法而不是textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
方法是使用
textView.getPaint().setUnderlineText(true);
And if you need to later turn off underlining for that view, such as in a reused view in a RecyclerView, textView.getPaint().setUnderlineText(false);
如果您稍后需要关闭该视图的下划线,例如在 RecyclerView 中的重用视图中, textView.getPaint().setUnderlineText(false);