Java 显示 HTML 格式的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2478503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:13:04  来源:igfitidea点击:

Display HTML Formatted String

javaandroideclipseandroid-arrayadapter

提问by Soren

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.

我需要一个示例,说明如何将我用简单 html 标记的字符串显示到 TextView 中。我找到了“Spanned fromHtml(String source)”,但我不知道如何将它插入到我的 java 代码中。

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

    }
}

Here is a sample of history:

下面是一个历史样本:

<?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>

Here is my historylistlayout.xml:

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

And here is my main.xml

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

回答by jqpubliq

Use the function you mentioned to return a spanned and then set the text of your textview using the toString of the returned Spanned object like so to achieve formatted text:

使用你提到的函数返回一个跨区,然后使用返回的跨区对象的 toString 设置你的文本视图的文本,像这样实现格式化文本:

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

This will work where yourTextWithHTML is any of the Strings within the item tags you posted. eg "<b>1883</b><br/>Some stuff happened"

这将适用于 yourTextWithHTML 是您发布的项目标签中的任何字符串的情况。例如"<b>1883</b><br/>Some stuff happened"

回答by shiami

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

回答by Artem Russakovskii

This seems easiest actually (from HTML in string resource?):

这实际上似乎最简单(来自字符串资源中的 HTML?):

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

This works for strings that have HTML inside.

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

回答by ninjasense

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:

我试图做一些类似的事情,发现我的 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");

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

它可以识别所有样式并正确格式化 html。更多来自 android 参考这里

回答by Simon

I faced the same problem, and solved it by using

我遇到了同样的问题,并通过使用解决了它

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

The above code returns styled text array associated with resource. It will return CharSequence[]

上面的代码返回与资源关联的样式文本数组。它将返回 CharSequence[]

Then just use the elemens in

然后只需使用元素

   setText(HistoryList[index]);