2013-02-09 67 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>))