我在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代替)


当前回答

为了防止有人发现这个问题,还有一个更好的替代方案没有被记录下来(我在搜索了几个小时后偶然发现了它,最终在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>

Android没有指定资源字符串的类型(例如text/plain或text/html)。但是,有一种变通方法允许开发人员在XML文件中指定这一点。

定义一个自定义属性,指定android:text属性为html。 使用子类化的TextView。

一旦你定义了这些,你就可以在xml文件中使用HTML来表达自己,而不必再次调用setText(HTML . fromhtml(…))。我很惊讶这个方法不是API的一部分。

这个解决方案的工作程度,Android工作室模拟器将显示文本作为渲染的HTML。

res/values/strings.xml (HTML格式的字符串资源)

<resources>
<string name="app_name">TextViewEx</string>
<string name="string_with_html"><![CDATA[
       <em>Hello</em> <strong>World</strong>!
 ]]></string>
</resources>

xml(只包含相关部分)

声明自定义属性名称空间,并添加android_ex:isHtml属性。也可以使用TextView的子类。

<RelativeLayout
...
xmlns:android_ex="http://schemas.android.com/apk/res-auto"
...>

<tv.twelvetone.samples.textviewex.TextViewEx
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/string_with_html"
    android_ex:isHtml="true"
    />
 </RelativeLayout>

Res /values/attrs.xml(为子类定义自定义属性)

 <resources>
<declare-styleable name="TextViewEx">
    <attr name="isHtml" format="boolean"/>
    <attr name="android:text" />
</declare-styleable>
</resources>

java (TextView的子类)

 package tv.twelvetone.samples.textviewex;

 import android.content.Context;
 import android.content.res.TypedArray;
 import android.support.annotation.Nullable;
 import android.text.Html;
 import android.util.AttributeSet;
 import android.widget.TextView;

public TextViewEx(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextViewEx, 0, 0);
    try {
        boolean isHtml = a.getBoolean(R.styleable.TextViewEx_isHtml, false);
        if (isHtml) {
            String text = a.getString(R.styleable.TextViewEx_android_text);
            if (text != null) {
                setText(Html.fromHtml(text));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        a.recycle();
    }
}
}

如果你想显示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))
                }

我有另一种情况,当我没有机会把CDATA放入xml,因为我从服务器接收字符串HTML。

以下是我从服务器上得到的信息:

<p>The quick brown&nbsp;<br />
fox jumps&nbsp;<br />
 over the lazy dog<br />
</p>

它看起来更复杂,但解决方法要简单得多。

private TextView textView;

protected void onCreate(Bundle savedInstanceState) { 
.....
textView = (TextView) findViewById(R.id.text); //need to define in your layout
String htmlFromServer = getHTMLContentFromAServer(); 
textView.setText(Html.fromHtml(htmlFromServer).toString());

}

希望能有所帮助! 灵

最新更新:

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));
}