2010-03-19 106 views
8

我需要一個如何顯示我已經用簡單的html標記到TextView中的字符串的例子。我發現「Spanned fromHtml(String source)」,但我不知道如何將它插入到我的java代碼中。顯示HTML格式字符串

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

這裏是我的主。 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> 

回答

12

使用功能你提到返回一個跨越,然後使用像這樣返回的跨區對象的toString實現格式化文本設置你的TextView的文本:

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

這將在哪裏工作yourTextWithHTML任何的您發佈的項目標籤中的字符串。 如"<b>1883</b><br/>Some stuff happened"

+1

這工作,如果你正在膨脹字符串資源,但如果你正在膨脹的字符串數組。字符串數組將刪除HTML格式。 – 2012-03-19 16:54:08

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

這似乎是最簡單的實際(從HTML in string resource?):

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

這適用於具有內HTML字符串。

13

我試圖做同樣性質的東西,發現我與CSS一起HTML沒有得到正確格式化,所以我把繩子,裝成這樣的網頁流量:

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

+1

像魅力一樣工作......謝謝!! +1 – Hiral 2012-04-20 05:27:17

0

我面臨同樣的問題,並通過使用

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

與資源相關聯的上述代碼返回樣式的文本陣列解決它。它將返回的CharSequence []

然後,只需使用elemens在

setText(HistoryList[index]);