2016-11-18 4 views
3

私はアンドロイドAppでTextViewでHTML文字列を表示しようとしています。 TextViewを使用してHTMLをインラインスタイルで表示するにはどうすればいいのでしょうか。 WebViewを使用できない理由があります。 GitHub上で動作するリポジトリがありますか?は、アンドロイドでtextview経由で純粋なhtmlコードを表示することが可能です

ありがとうございます。ちょうどあなたの懸念のために、我々はあなたがテキスト

例として、あなたのtextViewでHTMLを使用するHtml.fromHtml()を使用する必要があるAPI 15

+1

が、TextViewにしかサポートしています限られた量のタグhttps://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html。私たちが知っていることは知っていますか? –

+0

それは私たちのPMからの不気味な要求です。 – Frank

答えて

4

に完璧にそれを実行する必要があります

textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>")); 
+0

インラインスタイルをサポートしなければなりません。 style = "font-size:20px; font-weight:600; color:#FF0000; text-ali gn:center; – Frank

+0

@Frank androidはすべてのHTMLタグを直接サポートしているわけではありません。 – AJay

+0

サポートされているタグの一覧はhttps://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html – AJay

2

私は私が得るソースを覚えていないが、私はこの方法を作った:

​​

xmlファイルにHTML文字列:TextViewに上の定義

<resources> 
    <string name="app_name">TestProject2</string> 

    <string name="html"> 
     <![CDATA[ 
     <h1>Main Title</h1> 
     <h2>A sub-title</h2> 
     <p>This is some html. Look, here\'s an <u>underline</u>.</p> 
     <p>Look, this is <em>emphasized.</em> And here\'s some <b>bold</b>.</p> 
     <p>This is a UL list: 
     <ul> 
     <li>One</li> 
     <li>Two</li> 
     <li>Three</li> 
     </ul> 
     <p>This is an OL list: 
     <ol> 
     <li>One</li> 
     <li>Two</li> 
     <li>Three</li> 
     </ol> 
     ]]> 
    </string> 
</resources> 

:あなたは "Html.fromHtml" を使用することができ

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context=".MainActivity" 
       android:orientation="vertical" 
       android:gravity="top|fill_vertical"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

    <!-- 
    <WebView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/webView" 
     android:layout_below="@+id/helloWorld" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     /> 
    --> 

</LinearLayout> 

そしてメインの活動に

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.text.Html; 
import android.text.Spanned; 
import android.widget.TextView; 

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // get our html content 
     String htmlAsString = getString(R.string.html);  // used by WebView 
     Spanned htmlAsSpanned = Html.fromHtml(htmlAsString); // used by TextView 

     // set the html content on a TextView 
     TextView textView = (TextView) findViewById(R.id.textView); 
     textView.setText(htmlAsSpanned); 

//  WebView webView = (WebView) findViewById(R.id.webView); 
//  webView.loadDataWithBaseURL(null, htmlAsString, "text/html", "utf-8", null); 

    } 

} 
+0

オプションはありません。インラインスタイルをサポートしなければなりません。 style = "font-size:20px; font-weight:600; color:#FF0000; text-align:center; – Frank

関連する問題