2010-03-19 3 views
8

간단한 html로 마크 업 한 문자열을 TextView에 표시하는 방법에 대한 예제가 필요합니다. 나는 "Spread fromHtml (String source)"을 발견했다. 그러나 그것을 나의 자바 코드에 연결하는 방법을 모른다.HTML 형식의 문자열 표시

다음
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"?> 
<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 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입니다

여기 내 자바입니다. xxx

<?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를 사용하여 텍스트 뷰의 텍스트를 설정 : yourTextWithHTML는 어떤 곳

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

이 작동합니다 귀하가 게시 한 항목 태그 내의 문자열. 예를 들어 "<b>1883</b><br/>Some stuff happened"

+1

에 elemens를 사용하여 반환합니다. 문자열 배열은 HTML 형식을 제거합니다. –

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

이 (HTML in string resource?에서) 실제로 가장 쉬운 것 같다

이 내부 HTML을 문자열을 작동
mTextView.setText(getText(R.string.my_styled_text)); 

.

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을 올바르게 포맷합니다. 안드로이드 기준 HERE

+1

는 매력처럼 작동했습니다 ... 감사합니다! +1 – Hiral

0

에서 더 많은 I 동일한 문제에 직면하고

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

자원과 관련된 위의 코드를 반환 스타일의 텍스트 배열을 사용하여 해결. 그것은의 CharSequence []를

가 그럼 그냥 당신이 문자열 배열을 팽창하고 있지 않은 경우지만, 문자열 리소스를 팽창하는 경우이 작동

setText(HistoryList[index]);