【问题标题】:Retaining whitespaces in textview in android在android的textview中保留空格
【发布时间】:2012-09-11 05:11:36
【问题描述】:

我正在尝试使用 html 对某些文本进行格式化(添加样式)并将其设置为 textview 的文本

Spanned text =Html.fromHtml(""+newAllText);
hello.setText(text, TextView.BufferType.SPANNABLE); 

但是 Html() 方法返回 Spanned 字符串,其中删除了所有空格。我尝试过使用诸如<pre> 之类的html标签,但没有帮助..

它应该像 this 一样很好地显示出来(html 标签后来用 from Html() 方法删除)但是所有的空格都被删除了,它看起来像 this (当它设置为ui 中的文本看起来像一个连续的大段落)。

有人对此有任何线索吗?

以防万一有人想查看完整代码:

String SelectedSentence =allText.substring(lastPoint, nextPoint);
String highlighted = "<font color='red'>"+SelectedSentence+"</font>";
StringBuilder newAllText = new StringBuilder(allText);
newAllText.delete(lastPoint, nextPoint+1);
newAllText.insert(lastPoint,highlighted);
newAllText = new StringBuilder("<pre>"+newAllText+"</pre>");

Spanned text =Html.fromHtml(""+newAllText);
hello.setText(text, TextView.BufferType.SPANNABLE); 
lastPoint = nextPoint;

【问题讨论】:

  • 删除之前的问题并再次询问不是一个好主意。如果有人知道,他/她会回答。你只需要等待。
  • 请参考这个网站,这样你就可以找到TEXT VIEW支持的所有HTML标签commonsware.com/blog/Android/2010/05/26/…
  • @harshit 好的,注意到了。
  • @droidhot 好名单!对我来说太糟糕了,没有任何有用的东西。我想我会尝试 android 的 Webview api 来代替。
  • @user1602020 抱歉,只有指定的标签在 android 文本视图中受支持,对此无能为力

标签: java android html whitespace


【解决方案1】:

好吧,我放弃了整个 HTML 文本样式方法,只是使用 SpannableStringBuilder 来获得我想要的相同效果

            highlighted = new SpannableStringBuilder(sb);
            highlighted.removeSpan(new BackgroundColorSpan(Color.YELLOW));
            highlighted.setSpan(new BackgroundColorSpan(Color.YELLOW),lastPoint, nextPoint, 0); 

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多