您当前的位置: 首页 > 

蓝不蓝编程

暂无认证

  • 0浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

安卓改变TextView中部分文字颜色

蓝不蓝编程 发布时间:2019-12-19 16:51:12 ,浏览量:0

效果图

实现方案
  1. 方案1: 通过html方式
private fun setTextByHtml() {
        val str = "默认颜色红颜色"
        textView1.text = Html.fromHtml(str)
    }
  1. 方案2: 通过SpannableString设置
    private fun setTextBySpannable() {
        val spannableString = SpannableString("默认颜色红颜色")
        val color = Color.parseColor("#FF0000")
        val startIndex = 4
        val endIndex = spannableString.length
        spannableString.setSpan(
            ForegroundColorSpan(color),
            startIndex,
            endIndex,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        textView2.text = spannableString
    }

#源代码 https://gitee.com/cxyzy1/colorTextView

关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0422s