【问题标题】:display html in textview with highlighted sentence/syntax在 textview 中显示带有突出显示的句子/语法的 html
【发布时间】:2016-08-12 23:05:15
【问题描述】:

嗨,我是 android 的初学者。但我写了一些句子和 C 代码,这些代码使用字体颜色等样式以 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
    }

【问题讨论】:

  • 您尝试过发布您的代码!所以每个人都会帮助你:)

标签: android html fonts colors textview


【解决方案1】:
    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);

【讨论】:

  • 它对我不起作用..使用此代码我的活动开始并自动关闭它而不显示任何文本
  • 使用网络视图 Insted Textview 它在 webView 中完美运行我检查了你的代码,但我不知道它在 textView 中不工作
  • 是的,我已经完成了。它在 android webview 中运行良好。在我的情况下,我想在 Textview 中显示。有什么办法可以在 TextView 中显示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-31
相关资源
最近更新 更多