【问题标题】:Html string with <style> tag and style in android textviewandroid textview中带有<style>标签和样式的html字符串
【发布时间】:2014-07-29 08:59:51
【问题描述】:

我试图在文本视图中显示一个 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>

我也尝试过 Spannable String,但它以相同颜色显示下划线和下划线文本。我不想更改文本颜色。

s.setSpan(new ForegroundColorSpan(Color.CYAN), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

谁能帮我创建自定义的textview,它可以支持textview中的标签?

【问题讨论】:

  • 这里你使用的是 CSS。 android 不支持 CSS,如果你想使用那么 Use Html.fromHtml("Your Text in tags");
  • 我用过这个方法。但我正在寻找自定义文本视图,它也可以支持 css。
  • TextView 不支持样式标签,所以尝试在 WebView 上加载。
  • 如果你使用 some text 就可以了
  • @Roshni 我认为您需要使用 WebView 而不是 TextView 以使其更容易。只需设计与 TextView 完全相同的 webview。

标签: javascript android html css textview


【解决方案1】:

对于 html,你必须使用这个:

mytext.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));

也检查这个链接 http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29

这是一个允许的 HTML 标签列表:

br
p
div
em
b
strong
cite
dfn
i
big
small
font
blockquote
tt
monospace
a
u
sup
sub

【讨论】:

  • 这是否支持内联样式?说&lt;p style='margin: 0; color: #ff0000;'&gt;This is a paragraph.&lt;/p&gt;
  • 不,只支持上面的标签。
【解决方案2】:

TextView 不支持 stylespan 标签。

你可以这样做:

String myHtmlString = Html.fromHtml("<u> <font color=\"#FF0000\"> some text </font> </u>");

仅供参考,here 是受支持标签的列表。

【讨论】:

    【解决方案3】:

    你应该这样使用

    String styledText = "This is <font color='red'>simple</font>.";
    textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
    

    【讨论】:

    • 它将以相同颜色显示下划线和下划线文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2011-08-25
    相关资源
    最近更新 更多