2014-06-08 117 views
5

我想使習慣號碼:粗體,我嘗試了HTML標記,但它不起作用。 我做了一些研究,但找不到任何。希望有人能夠幫助我。謝謝!在Java中創建一個字符串文本粗體Android

String habitnumber = "Habit Number: " + String.valueOf(habitcounter); 
String Title = habit.getTitle(); 
String description = habit.getDescription(); 


//Set text for the row 
       tv.setText(habitnumber+ "\n" + Title + " \n" + description + "\n --------------------"); 
+0

裏面你是如何最終顯示它在屏幕上?這會產生很大的差異 –

+0

@GabeSechan編輯我的問題,現在看它 – meskh

+1

您需要用粗體樣式將其設置爲spannable。或者,您可以添加粗體html標籤,然後使用Html.fromHtml獲取帶有由html形成的spannable的字符串。 –

回答

15
I tried the HTML tag but it didn't work 

我認爲你沒有包裝你的字符串Html.fromHtml

解決方案:

您可以使用HTML標記來使用HTML:bold:<b>

String habitnumber = "<b>" + "Habit Number: " + "</b> " + String.valueOf(habitcounter); 
tv.setText(Html.fromHtml(habitnumber)); 
1
tv.setTypeface(null, Typeface.BOLD); 
+0

問題在於它使整個textview變爲粗體。這可能不是他想要的。 –

+0

所以也許textview中的textview?什麼問題 ? – JaKoZo

0

像加布提到,嘗試使用使用Html.fromHtml或使用spannable。 檢查了這一點:Selecting, Highlighting, or Styling Portions of Text

你基本上可以使用HTML標籤在你的字符串資源,如:

<resource> 
<string id="@+id/styled_welcome_message"><b>your text</b></string> 
</resources> 
0

使用此XML文件中,TextView的

android:textStyle="bold"