android中实现为textview添加多个可点击的文本,如何在一个TextView中实现多种文本风格?...

这篇博客探讨了如何在Android的TextView中为不同部分的文本设置多种风格,如加粗、斜体等。通过使用Spannable和相关Span类,可以实现对文本的精细化样式控制,尽管这种方法在文本中的应用可能受到限制。示例代码展示了如何创建和应用不同的文本样式,包括设置字体样式和背景颜色。
摘要由CSDN通过智能技术生成

可以为TextView中不同部分的文本设置多种风格(style)吗?

例如,我按照下述方式设置文本:

tv.setText(line1 + "\n" + line2 + "\n" + word1 + "\t" + word2 + "\t" + word3);

每个文本元素都能匹配不同的风格效果吗?例如,第一行,加粗,第一个字,斜体等等。在开发者指南Common Tasks and How to Do Them in Android中包括Selecting, Highlighting, or Styling Portions of Text的方法:

// 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 storage

Spannable 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);

但这种方法在文本中的使用次数受限,所以,有什么方法可以达到我想要的效果吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值