2013-02-09 64 views
0

我创建了简单的文本视图应用程序。它工作正常,我需要在文本视图中解析字符串值。因为我有一个XML文件,我的XML文件包含一些空的标签,如果我读了空标签,我需要在我的文本视图中加粗视图例如:这是我的标签<Strong> </Strong>如果我阅读这个标签,我希望在我的文本视图中添加大胆的视图,所以我想在我的文本视图中添加大胆的视图。如何在textview字体中解析字符串值?

XML文件:

 <text> 
<![CDATA[<p style="text-align: center;"> 
<span style="color:#FFFFFF;"> 
<strong> 
<span style="font-size:30px;"> 
<span style="font-family:georgia,serif;"> 
<span style="color: rgb(0, 128, 128);"> 
welcome </span> 
</span> 
</span> 

</strong> 
</span></p> 
]]> 
       </text> 

代码:

NodeList Bold_value = tt.getElementsByTagName("strong"); 

      if (Bold_value.getLength() > 0) { 
       //Style(); 
       bod = "Typeface.BOLD"; 
} 

尝试添加在文本视图大胆的观点:

TextView txt = (TextView)findViewById(R.id.sample); 

     txt.setBackgroundColor(Color.parseColor(color)); 

     txt.setTypeface(null, bod); 

该行正试图解析值:

txt.setTypeface(null, bod); 

回答

0

setTypeFace需要一个int作为参数(不是字符串)。

,所以你可以这样做:

txt.setTypeFace(txt.getTypeface(),Typeface.BOLD); 

,或者作为一种替代方案:

int bod = Typeface.NORMAL; 
if (Bold_value.getLength() > 0) { 
      //Style(); 
      bod = Typeface.BOLD; 
} 
txt.setTypeFace(txt.getTypeface(),bod); 

(并保持目前的字样,而不是传球空)

+0

我已经尝试过你的第二个,但我在这一行中出现错误为什么? Style.BOLD和NORMAL错误:NORMAL无法解析或不是字段,BOLD无法解析或不是字段 – 2013-02-09 09:38:24

+0

oups ...抱歉,我的错误。使用字体,而不是样式(我固定我的帖子) – ben75 2013-02-09 09:49:55

+0

感谢兄弟.... – 2013-02-09 10:03:42

0

你的数据看起来非常像html一样,你可以使用:

myTextView.setText(Html.fromHtml(<html string>))