首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓: strings.xml中的超文本标记语言

安卓: strings.xml中的超文本标记语言
EN

Stack Overflow用户
提问于 2012-11-17 05:56:10
回答 5查看 82.4K关注 0票数 96

例如,我想显示以下html代码:

代码语言:javascript
复制
<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>

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

EN

回答 5

Stack Overflow用户

发布于 2012-12-16 00:21:13

在strings.xml中添加html源代码的最好方法是使用<![CDATA[html source code]]>。下面是一个示例:

代码语言:javascript
复制
<string name="html"><![CDATA[<p>Text</p>]]></string> 

然后,您可以使用以下命令在TextView中显示此html:

代码语言:javascript
复制
myTextView.setText(Html.fromHtml(getString(R.string.html)));

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

代码语言:javascript
复制
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
票数 228
EN

Stack Overflow用户

发布于 2012-11-17 06:27:24

以下是大多数示例。我不认为pre标签被支持。

这是strings.xml文件:

代码语言:javascript
复制
<?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>

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

代码语言:javascript
复制
<?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>

最后,代码:

代码语言:javascript
复制
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)));
票数 27
EN

Stack Overflow用户

发布于 2012-11-17 06:15:46

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

代码语言:javascript
复制
<resources>
    <string name="hello_world">&lt;span&gt;</string>
</resources>

在您的代码中:getResources().getString(R.string.hello_world);的计算结果为"<span>"。您可以像这样使用此HTML格式的文本:

代码语言:javascript
复制
TextView helloWorld = (TextView)findViewById(R.id.hello_world);
helloWorld.setText(Html.fromHtml(getString(R.string.hello_world)));
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13425002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档