2013-02-12 59 views
29
Tweet o = tweets.get(position); 

TextView tt = (TextView) v.findViewById(R.id.toptext); 
//TextView bt = (TextView) v.findViewById(R.id.bottomtext);   

EditText bt =(EditText)findViewById(R.id.bottomtext); 
bt.setText(o.author); 
Spannable spn = (Spannable) bt.getText(); 
spn.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC) 
, 0, 100, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

//bt.setText(o.author);  
tt.setText(o.content); 

我在我的Android應用程序中設置twitter數據。我想使用Spannable使字體粗體和斜體,但它不起作用,出現錯誤。我該怎麼做?spannable on android for textView

+0

它給出的意外停止錯誤。通常代碼工作,但我想使字體粗體。 ıtgıves此錯誤。 – baran 2013-02-12 19:41:39

+0

請發佈LogCat輸出,但例外。最有可能你嘗試跨越文本結束應用跨度 - 使用實際長度而不是100 – Asahi 2013-02-12 19:53:31

+0

這種方法可能會有所幫助https://stackoverflow.com/a/44255840/3496570 – Nepster 2017-05-30 07:24:01

回答

69

我想使字體粗體和斜體與spannable

爲這款U將需要o.content文本SpannableString然後將其設置爲TextView的爲:

SpannableString spannablecontent=new SpannableString(o.content.toString()); 
spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 
         0,spannablecontent.length(), 0); 
// set Text here 
tt.setText(spannablecontent); 

編輯: 您還可以使用Html.fromHtml在文本視圖中將文本粗體和斜體設置爲:

tt.setText(Html.fromHtml("<strong><em>"+o.content+"</em></strong>")); 
+0

spannablecontent.setSpan(新StyleSpan(android。 graphics.Typeface.BOLD_ITALIC), 0,spannablecontent.length(),0); – baran 2013-02-12 19:55:37

+0

解決我的疑問問題 – baran 2013-02-12 19:55:59

+2

@baran:你也可以使用'Html.fromHtml'使文字變粗體和斜體 – 2013-02-12 19:57:50

1

最簡單的方法來創建一個spannable文本bolditalic設置成你的TextView使用方法Html.fromHtml()

,並使用html元素<b><i>

myTextView.setText(Html.fromHtml("<b><i>my text bold and italic!</i></b>")); 

或HTML元素<strong><em>

myTextView.setText(Html.fromHtml("<strong><em>my text bold and italic!</em></strong>")); 
+0

爲什麼-1?任何原因。 – Jorgesys 2016-10-21 03:11:46

相關問題