Android:在 TextView 中为动态文本静态添加下划线
我想让 TextView 完全带有下划线,但我无法使用文本资源和 标记,因为它是动态文本。
到目前为止,我知道做到这一点的唯一方法是在运行时。这真的是唯一的方法吗?有什么办法可以在 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的解决方案可能是创建一个从 TextView 派生的自定义 UnderLineTextView 组件,覆盖 setText() 并将整个文本设置为下划线,如下所示(来自您上面提到的链接的下划线代码):
然后只需使用您的布局中的新组件并照常设置文本。其余的将自动处理。
有关自定义组件的更多信息:
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):
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
您可以在 Html.fromHtml(String来源)
示例:
You can underline text over Html.fromHtml(String source)
Example:
如果您愿意,也可以通过
/res/values/string.xml
文件执行此操作:例如,在/res/values/string.xml
中,您可以添加像这样的条目:然后在活动的 onCreate(Bundle savingInstanceState) 方法中,您将添加以下代码以导致“创建帐户”>在您为 createAccountText TextView 设置的 UI 中显示为下划线,该 TextView 是您在 Activity 的
/res/layout/
的 xml 文件中定义的: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: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: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.