显示HTML格式字符串

9

我需要一个示例,展示我用简单的html标记好的字符串如何显示到TextView中。我已经找到了"Spanned fromHtml(String source)",但是我不知道如何将它插入到我的Java代码中。

这是我的Java代码:

package com.SorenWinslow.TriumphHistory;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class TriumphHistory extends ListActivity {
    String[] HistoryList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayAdapter<String> adapter;
        HistoryList = getResources().getStringArray(R.array.history);
        adapter = new ArrayAdapter<String> (this,R.layout.historylistlayout,HistoryList);
        setListAdapter(adapter);

    }
}

这里是历史示例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="history">
<item><b>1883</b><br/>Some stuff happened</item>
<item><b>1884</b><br/>Some more stuff happened <i>before</i> the other stuff
</item>
<resources>

这是我的historylistlayout.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:textColor="#ffffff"
    android:background="#000050"
    android:layout_marginTop="5px"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:padding="3px" 
    android:textSize="8pt" android:layout_gravity="top|left"/>

这是我的main.xml文件:

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:textColor="#ffffff"
    android:background="#000080" 
    android:isScrollContainer="true" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" android:scrollbarStyle="insideOverlay">

  <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:dividerHeight="1px"/>

</LinearLayout>
5个回答

13

我试图做一些相似的事情,并发现我的HTML和CSS没有得到正确的格式化,所以我将字符串加载到了一个Webview中,就像这样:

 WebView webview = (WebView) findViewById(R.id.MyWebview);
 String summary = "<html><body>You scored <b>192</b> points.</body></html>";
 webview.loadData(summary, "text/html", "utf-8");

而且它识别了所有的样式并正确格式化了html。更多内容请参见此处的Android参考文档。


12

使用您提到的函数返回一个 Spanned 对象,然后使用返回的 Spanned 对象的 toString 方法设置您的 TextView 的文本,以实现格式化文本:

Spanned marked_up = Html.fromHtml(yourTextWithHTML);
((TextView) findViewById(R.id.text1)).setText(marked_up.toString());

如果yourTextWithHTML是您发布的item标签中的任何一个字符串,那么这将起作用。例如:"<b>1883</b><br/>Some stuff happened"


1
如果您正在膨胀字符串资源,则此方法有效,但如果您正在膨胀字符串数组,则无效。字符串数组将删除HTML格式。 - Jason Robinson

8
    Spanned spannedContent = Html.fromHtml(htmlString);
    textView.setText(spannedContent, BufferType.SPANNABLE);

1

这似乎是最简单的方法(来自字符串资源中的HTML?):

mTextView.setText(getText(R.string.my_styled_text));

这适用于包含 HTML 的字符串。


0

我也遇到了同样的问题,通过使用以下方法解决了它

    CharSequence[] HistoryList = getResources().getTextArray(R.array.history);

以上代码返回与资源相关联的样式化文本数组。它将返回CharSequence[]。
然后只需使用元素即可。
   setText(HistoryList[index]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接