Android:strings.xml中的html

Alb*_*rto 87 html xml string android android-resources

我想显示例如这个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>
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*lev 209

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

<string name="html"><![CDATA[<p>Text</p>]]></string> 
Run Code Online (Sandbox Code Playgroud)

然后你可以使用以下方法在TextView中显示这个html:

myTextView.setText(Html.fromHtml(getString(R.string.html)));
Run Code Online (Sandbox Code Playgroud)

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

myTextView.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)

  • 是的,但是使用`CDATA`,您包含的实际HTML更容易 - 无需翻译所有<,>等.只需复制真实的HTML,然后粘贴到您的strings.xml中 (15认同)
  • 如果你只使用`getText()`而不是`getString()`,你可以使用HTML _without_ CDATA:http://stackoverflow.com/a/18199543/89818 (8认同)
  • 选择您想要CDATA的文本..并按ctrl + alt + T - >选择'带有CDATA部分的Surrounf' (7认同)

wsa*_*lle 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>
Run Code Online (Sandbox Code Playgroud)

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

<?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>
Run Code Online (Sandbox Code Playgroud)

最后,代码:

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)));
Run Code Online (Sandbox Code Playgroud)


Tho*_*mas 5

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

<resources>
    <string name="hello_world">&lt;span&gt;</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

在您的代码中: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)));
Run Code Online (Sandbox Code Playgroud)