我在strings.xml中有一些固定的字符串,比如:

<resources>
    <string name="somestring">
        <B>Title</B><BR/>
        Content
    </string>
</resources>

在我的布局中,我有一个TextView,我想用html格式的字符串填充。

<TextView android:id="@+id/formattedtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/htmlstring"/>

如果我这样做,formattedtext的内容只是一些字符串的内容剥离任何html标签,因此未格式化。

我知道可以用编程方式设置格式化文本

.setText(Html.fromHtml(somestring));

因为我在程序的其他部分使用了这个,在那里它是正常工作的。

要调用这个函数,我需要一个Activity,但目前我的布局只是一个简单的或多或少静态的纯XML视图,我宁愿让它这样,以节省我创建一个活动的开销,只是设置一些文本。

我是不是忽略了一些显而易见的东西?难道根本不可能吗?欢迎任何帮助或解决办法!

编辑:刚刚尝试了一些东西,似乎HTML格式在xml有一些限制:

标签必须小写 这里提到的一些标签不起作用,例如<br/>(可以使用\n代替)


当前回答

最新更新:

Html.fromHtml(string);// Android N版本后不再使用

以下代码支持android N及以上版本…

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(yourHtmlString,Html.FROM_HTML_MODE_LEGACY));
}

else 
{
textView.setText(Html.fromHtml(yourHtmlString));
}

其他回答

为了防止有人发现这个问题,还有一个更好的替代方案没有被记录下来(我在搜索了几个小时后偶然发现了它,最终在Android SDK本身的漏洞列表中找到了它)。你可以在strings.xml中包含原始HTML,只要你把它包装起来

<![CDATA[ ...raw html... ]]>

边界情况:

Characters like apostrophe ('), double-quote ("), and ampersand (&) only need to be escaped if you want them to appear in the rendered text AS themselves, but they COULD be plausibly interpreted as HTML. ' and " can be represented as\' and \", or &apos; and &quot;. < and > always need to be escaped as &lt; and &gt; if you literally want them to render as '<' and '>' in the text. Ampersand (&) is a little more complicated. Ampersand followed by whitespace renders as ampersand. Ampersand followed by one or more characters that don't form a valid HTML entity code render as Ampersand followed by those characters. So... &qqq; renders as &qqq;, but &lt;1 renders as <1.

例子:

<string name="nice_html">
<![CDATA[
<p>This is a html-formatted \"string\" with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph from the same \'string\'.</p>
<p>To be clear, 0 &lt; 1, & 10 &gt; 1<p>
]]>
</string>

然后,在你的代码中:

TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml(getString(R.string.nice_html), FROM_HTML_MODE_LEGACY));

恕我直言,这是几个数量级更好的工作:-)


2021年8月更新:我最初的答案使用了Html.fromHtml(String),这在API 24中已弃用。建议使用fromHtml(String,int)表单作为其替代品。

FROM_HTML_MODE_LEGACY可能工作…但是对于您想要做的事情,其他标志之一可能是更好的选择。

最后一点,如果你更喜欢渲染Android跨文本适合使用Markdown语法而不是HTML在TextView中使用,现在有多个第三方库,包括https://noties.io/Markwon。

转义你的HTML标签…

<resources>
    <string name="somestring">
        &lt;B&gt;Title&lt;/B&gt;&lt;BR/&gt;
        Content
    </string>
</resources>

如果你想显示html scrip在android应用程序像TextView

请遵循以下代码

科特林

var stringvalue = "Your Sting"

yourTextVew.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(stringvalue, Html.FROM_HTML_MODE_COMPACT)
        } else {
            Html.fromHtml(stringvalue)
        }

Java

String stringvalue = "Your String";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                   yourTextVew.setText(Html.fromHtml(stringvalue, Html.FROM_HTML_MODE_COMPACT))
                } else {
                   yourTextVew.setText( Html.fromHtml(stringvalue))
                }

最新更新:

Html.fromHtml(string);// Android N版本后不再使用

以下代码支持android N及以上版本…

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(yourHtmlString,Html.FROM_HTML_MODE_LEGACY));
}

else 
{
textView.setText(Html.fromHtml(yourHtmlString));
}
  String termsOfCondition="<font color=#cc0029>Terms of Use </font>";
  String commma="<font color=#000000>, </font>";
  String privacyPolicy="<font color=#cc0029>Privacy Policy </font>";
  Spanned text=Html.fromHtml("I am of legal age and I have read, understood, agreed and accepted the "+termsOfCondition+commma+privacyPolicy);
        secondCheckBox.setText(text);