【问题标题】:Display HTML Formatted String显示 HTML 格式的字符串
【发布时间】:2010-03-19 15:30:20
【问题描述】:

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

【问题讨论】:

    标签: java android eclipse android-arrayadapter


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 这适用于对 String 资源进行膨胀,但在对 String 数组进行膨胀时无效。字符串数组将删除 HTML 格式。
      【解决方案3】:
          Spanned spannedContent = Html.fromHtml(htmlString);
          textView.setText(spannedContent, BufferType.SPANNABLE);
      

      【讨论】:

        【解决方案4】:

        这实际上似乎最简单(来自HTML in string resource?):

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

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

        【讨论】:

          【解决方案5】:

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

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

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

          然后只需使用

          中的元素
             setText(HistoryList[index]);
          

          【讨论】:

            猜你喜欢
            • 2011-01-05
            • 2019-03-04
            • 1970-01-01
            • 2021-11-28
            • 1970-01-01
            • 2022-01-20
            • 1970-01-01
            • 2013-01-29
            • 2020-03-13
            相关资源
            最近更新 更多