在android中为文本视图添加多种颜色

时间:2015-10-08 06:43:42

标签: android colors textview

我在新闻应用程序上工作我的json响应是html格式,因为它有多种颜色。我需要在单个文本视图中显示此文本。我的回答是这样的,

10-08 10:31:36.041: I/System.out(7242): <p><span style="font-size: xx-large;"><span style="font-family: EENADU;"><span style="color: #000099;">'&AElig;&cent;&Yuml;&iquest;&laquo;&Ouml;.. F &aelig;X&ecirc;&ordf;-N&Otilde;&scaron;&Euml; &AElig;&cent;&Yuml;&iquest;&laquo;&Ouml;??&Ntilde; &AElig;&macr;&auml; KA&copy;&eth; &Ecirc;&Egrave;-P&Egrave;X&frac34;&ordf;&frac12;u&cent;&Aring;&OElig;&cent; &sup2;&ugrave;&cent;&Yuml;&iquest;-&ordf;&frac12;u&cent;&Aring;&icirc; &bdquo;&ccedil;&Otilde;J-&reg;&Iuml;-&curren;&ograve;-&bdquo;&Atilde;-&copy;&macr;&auml; &sbquo;&ldquo;&Aring;&OElig;&Otilde;-&Aring;&OElig;-&Aring;&icirc; &Iacute;&Atilde;&copy;&Ccedil;-&laquo;&Otilde;&cent;C &aelig;X&para;&dagger;&Iuml;-&sect;&OElig;&Otilde;&copy;&uuml;q, X&Ocirc;&copy;&uuml;q &AElig;&cent;{&Ouml; &brvbar;&Ouml;u&scaron;&Igrave;-&curren;&Auml;-&ordf;&frac12;x-&ordf;&frac12;x&Ecirc;&Otilde; &sbquo;&ldquo;&not;&Aacute;-&ordf;&aacute;-&sup2;&Auml;h&ordf;&frac12;&Otilde;. &AElig;&ordf;&aacute;&Aring;&auml; &AElig;&cent;&Aring;&OElig;&scaron;&Euml; &AElig;&cent;&Yuml;&iquest;&cent; &sup2;&ntilde;&cent;&Aring;&OElig;&cent; &Iacute;&auml;&reg;&frac34;&Otilde;&Acirc;&icirc;&bdquo;&Atilde;&copy;&cent;&tilde;&auml; &curren;&Auml;&ordf;&frac12;x-&ordf;&yacute;&copy;&eth; &aelig;X&para;&dagger;&Iuml;-&sect;&OElig;&Otilde;&copy;&uuml; &laquo;&Ouml;&ldquo;&Aring;&OElig;&bdquo;&auml;&Otilde; &Iacute;&auml;&ordf;&aacute;&cent;-&Iacute;&OElig;&Otilde;-&Acirc;&sup1;&times;&cent;&tilde;&auml; &reg;&frac34;J-&curren;&ograve;&Yuml;&iquest;&Otilde;. &Yuml;&Auml;E&Acirc;&Euml; &laquo;&aacute;&cent;&Yuml;&iquest;&Otilde;, &Aring;&OElig;&ordf;&Atilde;y&Aring;&OElig; &Acirc;&sup1;&Oslash;&oelig;&Auml; &Acirc;&iacute;Eo &egrave;&Ccedil;&ldquo;&rsquo;&sup1;-&Aring;&OElig;h&copy;&Otilde; B&reg;&frac34;&Otilde;-&Acirc;&sup1;&times;&cent;-&tilde;&auml;&macr;&auml; &sbquo; &aelig;X&para;&dagger;&Iuml;-&sect;&OElig;&Otilde;&copy;&uuml; X&frac34;&Uuml;Jh &reg;&frac34;&Aring;&OElig;p&acute;-L-&Aring;&Atilde;&copy;&Ecirc;&Otilde; &fnof;&reg;&frac34;&Otilde;h&cent;C. &AElig;&bdquo;&auml;&cent;&scaron;&eth; &Aring;&ccedil;&copy;&Otilde;-&reg;&frac34;&Otilde;-&Acirc;&icirc;-&bdquo;&Atilde;-&copy;&cent;&tilde;&auml; &fnof;C &Iacute;&OElig;C-&bdquo;&auml;-&sect;&OElig;&Ouml;-Lq&cent;&Yuml;&auml;..</span></span></span><br /><span style="font-size: x-large;"><span style="font-family: EENADU;"><span style="color: #000000;">&AElig;&cent;&Yuml;&iquest;&cent;&rsquo;&Atilde; &Acirc;&sup1;E-X&Iuml;&cent;-&Iacute;&Atilde;-&copy;E &sbquo;&not;&Aacute;-X&frac34;-&oelig;&iquest;E &AElig;A-&laquo;&copy;&Otilde; &hellip;&cent;&oelig;&iquest;-&ordf;&frac12;&cent;&tilde;&auml; &AElig;A-&not;&Aacute;-&sect;&ccedil;&Ouml;&Acirc;&Euml;h &Acirc;&Atilde;&Yuml;&auml;&bdquo;&ccedil;&Ouml;! &AElig;&cent;&Yuml;&iquest;&Otilde;&ecirc;&Acirc; &sup2;&ugrave;&cent;&Yuml;&iquest;&ordf;&frac12;u &reg;&frac34;&cent;&ordf;&frac12;-&Acirc;&sup1;~-&ordm;&copy;&eth; &Aring;&OElig;&copy; &Ecirc;&Otilde;&cent;* &curren;&Auml;&Yuml;&Auml;&copy; &laquo;&ordf;&frac12;&Acirc;&sup1;&times; &reg;&frac34;&laquo;&Otilde;&reg;&frac34;h&cent; &laquo;&aacute;&Egrave;u&cent;-&rsquo;&Atilde;&macr;&auml; &brvbar;&micro;&Ccedil;N-&sup2;&Auml;h&ordf;&frac12;&Otilde;. &uml; &ldquo;&Acirc;&sup1;&laquo;&Otilde;&cent;&copy;&eth; &Aring;&OElig;&laquo;&Otilde; &laquo;&aacute;&Egrave;&cent; &laquo;&Otilde;J&cent;&Aring;&OElig; &ldquo;X&frac34;&Acirc;&Atilde;-&not;&Aacute;-&laquo;&cent;-&Aring;&OElig;&cent;&rsquo;&Atilde; &Acirc;&sup1;E-X&Iuml;&cent;-&Iacute;&Atilde;-&copy;E &ordf;&frac12;&Acirc;&sup1;-&ordf;&frac12;-&Acirc;&Atilde;&copy; &aelig;X&para;&dagger;&Iuml;-&sect;&OElig;&Otilde;&copy;&uuml;q &aring;&reg;j&Aring;&OElig;&cent; &Iacute;&auml;&ordf;&aacute;&cent;-&Iacute;&OElig;&Otilde;-&Acirc;&sup1;&times;&cent;{&Ouml; &hellip;&cent;&scaron;&Ccedil;&ordf;&frac12;&Otilde;. &AElig;&ordf;&aacute;&Aring;&auml; &bdquo;&Atilde;&scaron;&Euml; X&frac34;&Uuml;Jh X&para;&frac34;L-&Aring;&Atilde;Lo &curren;&ntilde;&cent;&Yuml;&Auml;-&copy;&cent;&tilde;&auml; &aelig;X&para;&dagger;&Iuml;-&sect;&OElig;&Otilde;&copy;&uuml; &Iacute;&auml;&ordf;&aacute;&cent;-& 

我的代码就像这样

fastionDesc=(TextView) v.findViewById(R.id.desc_desc);
String fidscription=newsobj.getString("articledec");
String parts[]=fidscription.toString().split("color");
for(int i=0;i<parts.length;i++) {
    if(i==1) {
        fidscription = fidscription.replaceAll(parts[i], "<font color='#000099'>" + parts[i] + "</font>");
    }
    if(i!=1&&i/2==0) {
        fidscription = fidscription.replaceAll(parts[i], "<font color='#000000'>" + parts[i] + "</font>");
    }
    if(i!=1&&i/2==1) {
        fidscription = fidscription.replaceAll(parts[i], "<font color='#ff0066'>" + parts[i] + "</font>");
    }
}
fastionDesc.setText((Html.fromHtml(fidscription)));
fastionDesc.setTypeface(tf);

当我这样做时,我收到了错误

10-08 12:10:23.432: E/AndroidRuntime(28414): java.util.regex.PatternSyntaxException: Error in {min,max} interval near index 1021:
10-08 12:10:23.432: E/AndroidRuntime(28414): : #333399;"><span style="font-family: EENADU;">&dagger;&Ocirc;&Acirc;&Atilde;-&Acirc;&Atilde;&sect;&OElig;&Otilde; &hellip;X&frac34;-&sect;&ccedil;&Ouml;T&cent;&Iacute;&OElig;&oelig;&iquest;&cent; &Acirc;&sup1;&times;&ordf;&frac12;&Otilde;&copy; &sbquo;&ordf;&icirc;-&rsquo;&Atilde;u-E&Acirc;&Euml; &laquo;&Otilde;&cent;*-&Yuml;&iquest;&Ecirc;o N&dagger;&frac34;&sect;&OElig;&Otilde;&cent; &Aring;&ccedil;L-&reg;&Iuml;&cent;&Yuml;&auml;. &AElig;&ordf;&aacute;&Aring;&auml; &ecirc;&Acirc;&laquo;&copy;&cent; &Acirc;&sup1;&times;&ordf;&frac12;&Otilde;&copy; N&dagger;&frac34;-&sect;&OElig;&Otilde;&cent;-&copy;&eth;&macr;&auml; &Acirc;&Atilde;&Yuml;&iquest;&Otilde;.. &Iacute;&OElig;&ordf;&Atilde;t-E&Acirc;&Euml; &Acirc;&sup1;&Oslash;&oelig;&Auml; &dagger;&Ocirc;&Acirc;&Atilde;-&Acirc;&Atilde;&sect;&OElig;&Otilde; &bdquo;&auml;&Otilde;&copy;&Otilde; &Iacute;&auml;&reg;&frac34;&Otilde;h&cent;C. DE&copy;&eth; &Iacute;&OElig;&ordf;&Atilde;tE&Acirc;&Euml; &bdquo;&auml;&Otilde;&copy;&Otilde; &Iacute;&auml;&aelig;&reg; N{-N&Otilde;&macr;&thorn; '&Dagger;&Ntilde;, '&reg;&Iuml;&Ntilde;, '&oelig;&Euml;&Ntilde;, &fnof;&Aring;&OElig;&ordf;&frac12; &sect;&OElig;&Ouml;&cent;&scaron;&Igrave;-&sbquo;-&Acirc;&Euml;q-&oelig;&ccedil;&cent;{&Otilde;x &reg;&frac34;&laquo;&Otilde;%-Cl&acute;&rsquo;&Atilde; &hellip;&cent;&scaron;&Ccedil;&ordf;&aacute;. &AElig;&copy;&Ccedil;&ecirc;&rsquo; &Acirc;&sup1;&times;&ordf;&frac12;&Otilde;&copy;&Otilde; &Yuml;&iquest;%&oelig;&micro;&iquest;&cent;&rsquo;&Atilde; &Aring;&OElig;&sect;&OElig;&Ouml;-&ordf;&frac12;-&laquo;-&oelig;&Auml;-E&Acirc;&Euml; &hellip;X&frac34;-&sect;&ccedil;&Ouml;-&rsquo;&sup1;-X&frac34;&oelig;&auml; N{-N&Otilde;&macr;&thorn; '&eacute;&Acirc;&Ntilde; &Acirc;&sup1;&Oslash;&oelig;&Auml; &copy;G&micro;-&reg;&frac34;&Otilde;h&cent;C. &fnof;{&Otilde; &Acirc;&sup1;&times;&ordf;&frac12;&Otilde;-&copy;&Acirc;&sup1;&times;, &AElig;{&Otilde; &Iacute;&OElig;&ordf;&Atilde;t-E&Acirc;&Euml; &Dagger;&macr;&icirc;o &ordf;&frac12;&Acirc;&Atilde;-&copy;&Otilde;&rsquo;&Atilde; &bdquo;&auml;&Otilde;&copy;&Otilde; &Iacute;&auml;&aelig;&reg; &dagger;&Ocirc;&Acirc;&Atilde;-&Acirc;&Atilde;-&sect;&OElig;&Otilde;&Ecirc;&Otilde; &Dagger;Eo N&Yuml;&micro;&Auml;-&copy;&Otilde;&rsquo;&Atilde; &bdquo;&Atilde;&oelig;&iquest;&Iacute;&icirc;a &Aring;&ccedil;&copy;&Otilde;-&reg;&frac34;&Otilde;-&Acirc;&sup1;&times;&cent;&Yuml;&Auml;&cent; &ordf;&frac12;&cent;&oelig;&Euml;..</span></span><br /><span style="font-size: xx-large;"><span style="font-family: EENADU;"><span style="

请帮助。

2 个答案:

答案 0 :(得分:1)

似乎html有一些错误,或者Html.fromHtml()不支持它。

尝试在webview上加载

WebView webview = new Webview(this);
webview.loadHtml(fidscription);

希望这有帮助。

答案 1 :(得分:0)

您的错误是因为replaceAll正在使用parts[i]作为正则表达式,而其中的某些内容存在无效的正则表达式。请改用replace。这应该会让你超过你发布的错误。