Android:strings.xml中的html


92

我想显示例如这个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在对话框上显示它strings.xml。我该怎么做?


Answers:


218

在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());

9
如果您只使用以下代码,则可以使用不带 CDATA的HTML :stackoverflow.com/a/18199543/89818getText()getString()
caw 2013年

16
是的,但是使用CDATA实际的HTML包含要容易得多-无需翻译所有<,>等。只需复制真实的HTML,然后粘贴到您的strings.xml中
Richard Le Mesurier

谢谢,效果很好。我只是想知道如何在textview中垂直居中放置文本。
Herman

7
选择要使用CDATA的文本,然后按ctrl + alt + T->选择“带有CDATA的Surrounf”部分
Prashant Jajal,2016年

抱歉,它不起作用。我发现唯一可以使用的字符串,u,i和Html.from的所有受支持标签的有效解决方案是wsanville的解决方案,因此请使用&lt和&gt来打开和关闭HTML标签。
彼得

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

谢谢上帝,可以使用&lt; 和&gt; 效果很好。
Torsten Ojaperv

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

3

Android文档中介绍了XML资源系统支持的所有样式。

字符串资源:格式和样式

此处包含的所有内容都可以使用并直接设置在上TextView。如果你需要使用另外的HTML标记,你将需要把原始的HTML(与转义字符&lt;&gt;并且这样的)到资源和负载在整个事情WebView


2

这为我工作:

<?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>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.