javascript android textview中带有<style>标签和样式的Html字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25011611/
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
Html string with <style> tag and style in android textview
提问by Roshni
I'm trying to show display an html string in a textview. I used the Html.fromhtml method to load html string textview. But It failed to parse tag. Here is my html string
我正在尝试在文本视图中显示一个 html 字符串。我使用 Html.fromhtml 方法来加载 html 字符串 textview。但它未能解析标签。这是我的 html 字符串
<style>u.style{color:#FF0000;}span.style2{color:#000000;}</style>
<u class="style"><span class="style2">some text</span></u>
I had tried Spannable String also, But It displays underline and underlined text in same color.I don't want to change text color.
我也尝试过 Spannable String,但它以相同的颜色显示下划线和带下划线的文本。我不想更改文本颜色。
s.setSpan(new ForegroundColorSpan(Color.CYAN), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Can anyone help me to create custom textview, which can support tag in textview?
谁能帮我创建自定义文本视图,它可以支持文本视图中的标签?
回答by meysam
for html you must use this :
对于 html,你必须使用这个:
mytext.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
also check this link http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29
还检查此链接 http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29
and This is a list of allowed HTML tags:
这是允许的 HTML 标签列表:
br
p
div
em
b
strong
cite
dfn
i
big
small
font
blockquote
tt
monospace
a
u
sup
sub
回答by G.T.
回答by Rohit
you should have to use like this
你应该像这样使用
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);