显示HTML格式的字符串 [英] Display HTML Formatted String

查看:221
本文介绍了显示HTML格式的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个例子,说明如何将简单的html标记的字符串显示到TextView中。我发现Spined fromHtml(String source),但我不知道如何将其插入到我的java代码中。

I need an example of how to display the strings that I have marked up with simple html into a TextView. I have found "Spanned fromHtml(String source)", but I don't know how to plug it into my java code.

这是我的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:

Here is my 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

And here is my 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如下:

I was trying to do something of the same nature and found out that my html along with CSS was not getting formatted correctly so I took the string and loaded it into a webview like this:

 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

and it recognized all the styles and formatted the html correctly. More from the android reference HERE

这篇关于显示HTML格式的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆