例如,我想显示以下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>
我想通过在资源strings.xml
中声明html来在对话框中显示它。我该怎么做呢?
发布于 2012-12-16 00:21:13
在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-11-17 06:27:24
以下是大多数示例。我不认为pre
标签被支持。
这是strings.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Formatting</string>
<string name="link"><b>Hello World</b> This is a test of the URL <a href="http://www.example.com/">Example</a></string>
<string name="bold"><b>This text is bold</b></string>
<string name="emphasis"><em>This text is emphasized</em></string>
<string name="sup">This is <sub>subscript</sub> and <sup>superscript</sup></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-17 06:15:46
String.xml可以包含实体,如下所示:
<resources>
<string name="hello_world"><span></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)));
https://stackoverflow.com/questions/13425002
复制相似问题