java - 显示 HTML 格式化字符串

标签 java android eclipse android-arrayadapter

我需要一个示例来说明如何将我用简单的 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>

最佳答案

我试图做一些相同性质的事情,但发现我的 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 引用 HERE

关于java - 显示 HTML 格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2478503/

相关文章:

java - 如何将 am-pm-of-day 格式化为 "AM/PM"而不是 "a.m./p.m."?

android - 图形布局窗口上出现奇怪的 Eclipse ADT 错误

Mac 上不存在 eclipse.ini 文件

java - Grails 3中CustomID的自动递增

java - 无法重新加载 Maven 项目 : Cannot reconnect. java.lang.RuntimeException: 无法重新连接

android - 如何获取 ViewModel?

java - 调用 zxing 扫描仪后,我返回到我的 Activity ,但在一个新实例中

java - Junit5 的@CsvFileSource 注释不适用于 Eclipse

java - 如何在 OpenCV 中显示 Mat 变量中存在的所有值?

java - 指向 Java LinkedList 节点的指针