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使字体粗体和斜体,但它不起作用,给出错误。我该怎么办?
答案 0 :(得分:74)
我想使用spannable
使字体变为粗体和talic
为此,您需要将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在textview中创建文本Bold和Italic:
tt.setText(Html.fromHtml("<strong><em>"+o.content+"</em></strong>"));
答案 1 :(得分:2)
创建spannable
文字bold
和italic
以设置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>"));
答案 2 :(得分:0)
SpannableString hint = new SpannableString(o.content.toString());
字体大小
hint.setSpan(new RelativeSizeSpan(0.9f), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
字体家族
hint.setSpan(new TypefaceSpan("sans-serif"), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
斜体
hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, s.length(), 0);
颜色
hint.setSpan(new ForegroundColorSpan(Color.GRAY), 0, hint.length(), 0);
粗体
hint.setSpan(new StyleSpan(Typeface.BOLD), 0, hint.length(), 0);
参考链接: https://www.programcreek.com/java-api-examples/index.php?api=android.text.style.RelativeSizeSpan