在textview中显示带有突出显示的句子/语法的html

时间:2016-04-20 10:25:50

标签: android html fonts colors textview

嗨,我是android的初学者。但我写了一些句子和C代码,使用像font-color这样的样式用HTML格式化。我试图在TextView中显示,它工作正常,但字体颜色只显示为黑色。 通过编写一个例子来帮助我解决这个问题。先谢谢你。

  

 tv = (TextView) findViewById(R.id.tv_display);
 String s = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" +
                "<html>\n" +
                "<head>\n" +
                "  <meta content=\"text/html; charset=ISO-8859-1\"\n" +
                " http-equiv=\"content-type\">\n" +
                "  <title></title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<p class=\"MsoNormal\"><span\n" +
                " style=\"color: rgb(0, 176, 80);\" lang=\"EN-US\">This is\n" +
                "C program\n" +
                "displaying in android TextView :-<o:p></o:p></span></p>\n" +
                "<p class=\"MsoNormal\"><span lang=\"EN-US\"><o:p>&nbsp;</o:p></span></p>\n" +
                "<p class=\"MsoNormal\"><span lang=\"EN-US\">#include&lt;<span\n" +
                " style=\"color: rgb(84, 141, 212);\">stdio.h</span>&gt;<o:p></o:p></span></p>\n" +
                "<p class=\"MsoNormal\"><span lang=\"EN-US\">#include&lt;<span\n" +
                " style=\"color: rgb(84, 141, 212);\">conio.h</span>&gt;<o:p></o:p></span></p>\n" +
                "<p class=\"MsoNormal\"><span lang=\"EN-US\">Void\n" +
                "main()<o:p></o:p></span></p>\n" +
                "<p class=\"MsoNormal\"><span lang=\"EN-US\">{<o:p></o:p></span></p>\n" +
                "<p class=\"MsoNormal\"><span lang=\"EN-US\"><span\n" +
                " style=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n" +
                "</span>Printf(&ldquo;<span\n" +
                " style=\"color: rgb(84, 141, 212);\">Hello android..!!</span>&rdquo;)<span\n" +
                " style=\"color: red;\">;</span><o:p></o:p></span></p>\n" +
                "<p class=\"MsoNormal\"><span lang=\"EN-US\"><span\n" +
                " style=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n" +
                "</span>getch()<span style=\"color: red;\">;</span><o:p></o:p></span></p>\n" +
                "<p class=\"MsoNormal\"><span lang=\"EN-US\">}<o:p></o:p></span></p>\n" +
                "</body>\n" +
                "</html>\n";
        tv.setText(Html.fromHtml(s));

我使用NVU创建了这个html。

我真的想在textview中显示如下:

This is C program displaying in android TextView :-//font color green

    #include<stdio.h> //stdio.h font color blue
    #include<conio.h> //conio.h font color blue
    Void main()
    {
        Printf(“Hello android..!!”);//hello android font color blue
        getch();//semi colon font color red
    }

1 个答案:

答案 0 :(得分:0)

    String text = "<font color='black'>Google</font><font color='red'>Android</font>";
    tvText.setText(Html.fromHtml(text));

使用webView完美加载数据

web.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);