is it possible to display pure html code via textview on android

后端 未结 2 1187
[愿得一人]
[愿得一人] 2021-01-21 06:16

I\'m trying to show HTML string with TextView on my android App. I\'m curious about that how could I make it possible to show HTML with inline style via TextView. There\'re some

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 06:35

    I don't remember the source that I get , but I made this way:

    // get our html content
    String htmlAsString = getString(R.string.html);
    Spanned htmlAsSpanned = Html.fromHtml(htmlAsString); // used by TextView
    
    // set the html content on the TextView
    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText(htmlAsSpanned);
    

    html string on a xml file :

    
        TestProject2
    
        
            Main Title
            

    A sub-title

    This is some html. Look, here\'s an underline.

    Look, this is emphasized. And here\'s some bold.

    This is a UL list:

    • One
    • Two
    • Three

    This is an OL list:

    1. One
    2. Two
    3. Three
    ]]>

    Defining on textview:

    
    
        
    
        
    
    
    

    And on main activity

    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);
    
        }
    
    }
    

提交回复
热议问题