40
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 将字体设置为粗体和斜体,但它不起作用,出现错误。我该怎么做?

4

3 回答 3

79

我想用 spannable 使字体粗体和斜体

为此,您需要制作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 中制作粗体和斜体文本:

tt.setText(Html.fromHtml("<strong><em>"+o.content+"</em></strong>"));
于 2013-02-12T19:40:32.250 回答
2

创建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>"));
于 2015-08-21T23:27:06.043 回答
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

于 2020-06-12T15:49:59.843 回答