Android:在 TextView 中为动态文本静态添加下划线

发布于 2024-10-12 01:17:39 字数 425 浏览 4 评论 0原文

我想让 TextView 完全带有下划线,但我无法使用文本资源和 标记,因为它是动态文本。

相关:我可以在 Android 布局中为文本添加下划线吗?

到目前为止,我知道做到这一点的唯一方法是在运行时。这真的是唯一的方法吗?有什么办法可以在 XML 文件中做到这一点吗?

I would like to make a TextView to be entirely underlined but I can't use a text resource and <u> tag because it is dynamic text.

Related: Can I underline text in an android layout?

So far the only way I know to do this is at runtime. Is this really the only way? Is there a way I could do it in the XML files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

镜花水月 2024-10-19 01:17:39

最简单的解决方案可能是创建一个从 TextView 派生的自定义 UnderLineTextView 组件,覆盖 setText() 并将整个文本设置为下划线,如下所示(来自您上面提到的链接的下划线代码):

@Override
public void setText(CharSequence text, BufferType type) {
    // code to check text for null omitted
    SpannableString content = new SpannableString(text);
    content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
    super.setText(content, BufferType.SPANNABLE);

}

然后只需使用您的布局中的新组件并照常设置文本。其余的将自动处理。

有关自定义组件的更多信息:
http://developer.android.com/guide/topics/ui/custom -components.html

The easiest solution is probably to create a custom UnderLineTextView component deriving from TextView, override setText() and set the entire text as underlined, something like this (underline code from the link you referred to above):

@Override
public void setText(CharSequence text, BufferType type) {
    // code to check text for null omitted
    SpannableString content = new SpannableString(text);
    content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
    super.setText(content, BufferType.SPANNABLE);

}

It's then just a matter of using your new component in the layout and setting the text as usual. The rest is handled automatically.

More info on custom components:
http://developer.android.com/guide/topics/ui/custom-components.html

我的痛♀有谁懂 2024-10-19 01:17:39

您可以在 Html.fromHtml(String来源)

示例:

textView.setText(Html.fromHtml("this is <u>underlined</u> text"));

You can underline text over Html.fromHtml(String source)

Example:

textView.setText(Html.fromHtml("this is <u>underlined</u> text"));
喵星人汪星人 2024-10-19 01:17:39

如果您愿意,也可以通过 /res/values/string.xml 文件执行此操作:例如,在 /res/values/string.xml 中,您可以添加像这样的条目:

<string name="createAccount"><u>Create Account</u></string>

然后在活动的 onCreate(Bundle savingInstanceState) 方法中,您将添加以下代码以导致“创建帐户”>在您为 createAccountText TextView 设置的 UI 中显示为下划线,该 TextView 是您在 Activity 的 /res/layout/ 的 xml 文件中定义的:

TextView createAccountText = (TextView) findViewById(R.id.createAccountText);
Resources res = getResources();
CharSequence styledText = res.getText(R.string.createAccount);
createAccountText.setText(styledText, TextView.BufferType.SPANNABLE);

You can also do this via the /res/values/string.xml file if you prefer: For example, in the /res/values/string.xml you could add an entry like:

<string name="createAccount"><u>Create Account</u></string>

And then in the onCreate(Bundle savedInstanceState) method of your activity you would add the following code to cause "Create Account"> to appear as underlined in the UI that you set for the createAccountText TextView that you defined in the xml file in /res/layout/ for your activity:

TextView createAccountText = (TextView) findViewById(R.id.createAccountText);
Resources res = getResources();
CharSequence styledText = res.getText(R.string.createAccount);
createAccountText.setText(styledText, TextView.BufferType.SPANNABLE);
十秒萌定你 2024-10-19 01:17:39

peter3之前写过扩展TextView类并重写setText方法。

该解决方案将不起作用,因为 setText 方法被标记为 FINAL。

http://developer.android.com/参考/android/widget/TextView.html#setText(java.lang.CharSequence)

不确定代码是否可以通过一些修改来工作。

peter3 wrote before to extend TextView class and override setText method.

This solution won't work as setText method is marked as FINAL.

http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

Not sure if the code could work with some modifications.

美人迟暮 2024-10-19 01:17:39
 SpannableString content = new SpannableString(name);
 content.setSpan(new UnderlineSpan(), 0, name.length(), 0);
 geometrical_textview.setText(content, TextView.BufferType.SPANNABLE);
 SpannableString content = new SpannableString(name);
 content.setSpan(new UnderlineSpan(), 0, name.length(), 0);
 geometrical_textview.setText(content, TextView.BufferType.SPANNABLE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文