2013-07-30 67 views
1

我有這是一個包含HTML標記這樣如何在Android佈局中顯示Html內容?

<h4>Standard Web Browser Components</h4> 
<p>Let&#8217;s lay out a few components of the modern day web browser:</p> 
<ul> 
<li>Parsing (HTML, XML, CSS, JavaScript)</li> 
<li>Layout</li> 
<li>Text and graphics rendering</li> 
<li>Image decoding</li> 
<li>GPU interaction</li> 
<li>Network access</li> 
<li>Hardware acceleration</li> 
</ul> 
<header> 
<h1 class="entry-title"><a href="/2012/chrome-canary-for-developers/">Chrome Canary for Developers</a></h1> 
<p class="meta"> 
<time datetime="2012-11-02T08:16:00+04:00" pubdate data-updated="true">Nov 2<span>nd</span>, 2012</time> 
</p> 
</header> 

我想在我的Android佈局解析這個PHP文件。我是這樣做的

webtext.setText(Html.fromHtml(content)); 

但我失去了內容的風格和顏色。如何在不丟失樣式的情況下在我的佈局中顯示。

+0

您的課程沒有定義。您可能會缺少樣式定義。 – Daniel

回答

3

,因爲只有以下的標籤,允許Html.fromHtml()

<a href="..."> 
<b> 
<big> 
<blockquote> 
<br> 
<cite> 
<dfn> 
<div align="..."> 
<em> 
<font size="..." color="..." face="..."> 
<h1> 
<h2> 
<h3> 
<h4> 
<h5> 
<h6> 
<i> 
<img src="..."> 
<p> 
<small> 
<strike> 
<strong> 
<sub> 
<sup> 
<tt> 
<u> 

參考http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

PS:

加載風格等在WebView中嘗試以下

WebView myWebView = (WebView) findViewById(R.id.yourWebViewId); 
String strHtml = "<html><head>" 
      + "<style type=\"text/css\">body{color: #ffdec2; background-color: #1F0C01;}" 
      + "</style></head>" 
      + "<body>" 
      + "<p align=\"justify\">"     
      .................... 
      .................... 
      .................... 
      + "</p> " 
      + "</body></html>"; 

myWebView.loadData(strHtml, "text/html", "utf-8"); 
+0

所以還有其他方法可以做到這一點。 所以我會得到像網頁的確切結果? (不使用webview) – jquery404

+0

@ jquery404嘗試我更新的答案! –