97

我想显示例如这个 html 代码:

<body>
    <p><b>Hello World</b></p>
    <p>This is a test of the URL <a href="http://www.example.com"> Example</a></p>
    <p><b>This text is bold</b></p>
    <p><em>This text is emphasized</em></p>
    <p><code>This is computer output</code></p>
    <p>This is<sub> subscript</sub> and <sup>superscript</sup></p>
</body>

我想通过在资源中声明 html 来在 Dialog 上显示它strings.xml。我该怎么做?

4

5 回答 5

232

在 strings.xml 中添加 html 源代码的最佳方法是使用<![CDATA[html source code]]>. 这是一个例子:

<string name="html"><![CDATA[<p>Text</p>]]></string> 

然后您可以使用以下方法在 TextView 中显示此 html:

myTextView.setText(Html.fromHtml(getString(R.string.html)));

如果您的 html 中有链接并且希望它们可点击,请使用以下方法:

myTextView.setMovementMethod(LinkMovementMethod.getInstance());
于 2012-12-15T16:21:12.510 回答
27

这是大多数示例。我认为不pre支持该标签。

在此处输入图像描述

这是strings.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Formatting</string>
    <string name="link">&lt;b&gt;Hello World&lt;/b&gt; This is a test of the URL &lt;a href="http://www.example.com/"&gt;Example&lt;/a&gt;</string>
    <string name="bold">&lt;b&gt;This text is bold&lt;/b&gt;</string>
    <string name="emphasis">&lt;em&gt;This text is emphasized&lt;/em&gt;</string>
    <string name="sup">This is &lt;sub&gt;subscript&lt;/sub&gt; and &lt;sup&gt;superscript&lt;/sup&gt;</string>
</resources>

这是布局。请注意,链接实际上是可点击的,需要做一些额外的工作:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/test1"
            android:linksClickable="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>
        <TextView
            android:id="@+id/test2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>
        <TextView
            android:id="@+id/test3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>
        <TextView
            android:id="@+id/test4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    </LinearLayout>
</ScrollView>

最后,代码:

TextView test1 = (TextView)findViewById(R.id.test1);
Spanned spanned = Html.fromHtml(getString(R.string.link));
test1.setMovementMethod(LinkMovementMethod.getInstance());
test1.setText(spanned);

TextView test2 = (TextView)findViewById(R.id.test2);
test2.setText(Html.fromHtml(getString(R.string.bold)));

TextView test3 = (TextView)findViewById(R.id.test3);
test3.setText(Html.fromHtml(getString(R.string.emphasis)));

TextView test4 = (TextView)findViewById(R.id.test4);
test4.setText(Html.fromHtml(getString(R.string.sup)));
于 2012-11-16T22:27:24.430 回答
6

String.xml 可以包含 HTML 实体,如下所示:

<resources>
    <string name="hello_world">&lt;span&gt;</string>
</resources>

在您的代码中:getResources().getString(R.string.hello_world);将评估为"<span>". 您可以像这样使用这个 HTML 格式的文本:

TextView helloWorld = (TextView)findViewById(R.id.hello_world);
helloWorld.setText(Html.fromHtml(getString(R.string.hello_world)));
于 2012-11-16T22:15:45.650 回答
3

XML 资源系统支持的所有样式都在 Android 文档中进行了说明。

字符串资源:格式化和样式

那里包含的任何东西都可以直接使用和设置TextView。如果您需要进一步使用 HTML 标记,则需要将原始 HTML(带有转义字符等)放入资源中,并将整个内容加载&lt;到.&gt;WebView

于 2012-11-16T22:21:12.810 回答
1

这对我有用:

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">Sangamner College</string>
<string name="about_desc"><![CDATA[In order to make higher education available in the rural environment such as of Sangamner, Shikshan Prasarak Sanstha was established in 1960. Sangamner College was established by Shikshan Prasarak Sanstha, Sangamner on 23rd January 1961 on the auspicious occasion of Birth Anniversary of Netaji Subhashchandra Bose.The Arts and Commerce courses were commenced in June 1961 and in June 1965 Science courses were introduced. When Sangamner College was founded forty years ago, in 1961, there was no college available to the rural youth of this region. <br><br></>The college was founded with the aim of upliftment of the disadvantageous rural youth in all respects. On one hand, we are aware of the social circumstances prevailing in the rural area where we are working. So, we offer the elective option to students, which are favourable to the local atmosphere. On the other hand, we want to academically empower the aspiring youth by offering vocational course in Computer Applications to students of Arts &amp; Commerce. B.B.A., B.C.A. and M.C.A. courses were started with the same purpose. “Think globally, act locally” is our guiding Principle.]]></string>

于 2015-12-06T05:02:45.470 回答