在TextView中可以有多个样式吗?是否可以为TextView中的不同文本设置多个样式?例如,我将文本设置为:tv.setText(line1 + "\n" + line2 + "\n" + word1 + "\t" + word2 + "\t" + word3);对于每个文本元素是否可能有不同的样式?例如,行1粗体,字1斜体等。开发商指南常见任务及其在Android中的实现方法包括选择、高亮显示或设计文本的部分:// Get our EditText object.EditText vw = (EditText)findViewById(R.id.text);// Set the EditText's text.vw.setText("Italic, highlighted, bold.");// If this were just a TextView, we could do:// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);// to force it to use Spannable storage so styles can be attached.// Or we could specify that in the XML.// Get the EditText's internal text storageSpannable str = vw.getText();// Create our span sections, and assign a format to each.str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);但在文本中使用显式位置号。有更干净的方法吗?
3 回答
![?](http://img1.sycdn.imooc.com/54584f8f00019fc002200220-100-100.jpg)
胡子哥哥
TA贡献1825条经验 获得超6个赞
万一有人想知道如何做到这一点,这里有一个方法:(再次感谢Mark!)
mBox = new TextView(context);mBox.setText(Html.fromHtml("<b>" + title + "</b>" + "<br />" + "<small>" + description + "</small>" + "<br />" + "<small>" + DateAdded + "</small>"));
![?](http://img1.sycdn.imooc.com/545862120001766302200220-100-100.jpg)
呼啦一阵风
TA贡献1802条经验 获得超6个赞
Html.fromHtml()
Spanned text = Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff");textView.setText(text);
- 3 回答
- 0 关注
- 550 浏览
添加回答
举报
0/150
提交
取消