当前位置: 代码网 > it编程>App开发>Android > Android如何在一个TextView中设置不同字体大小、不同字体颜色封装

Android如何在一个TextView中设置不同字体大小、不同字体颜色封装

2024年07月23日 Android 我要评论
一、概述  在开发过程中遇到过这样一种业务,有很多单行文本字体。字符串中每一部分的字体样式、大小、颜色都不相同。传统的做法是放多个textview以达到效果。  但是当这个页面中的这样的元素非常多,且

一、概述

  在开发过程中遇到过这样一种业务,有很多单行文本字体。字符串中每一部分的字体样式、大小、颜色都不相同。传统的做法是放多个textview以达到效果。

  但是当这个页面中的这样的元素非常多,且非常复杂的时候,就会出现页面加载缓慢的问题(view加载=深度(递归)+平铺),也就是页面元素越多,层级越深加载速度越慢。

  此时如果能把本来要用四五个textview才能完成的事情用一个textview完成,这样就能大大提高页面加载速度。

  示例图:

二、代码示例(直接复制粘贴可用)

1.封装类:colorsizetextview.kt

package com.yw.custommutilimageadapter.widget
import android.content.context
import android.graphics.color
import android.text.spannablestring
import android.text.spanned
import android.text.style.absolutesizespan
import android.text.style.foregroundcolorspan
import android.util.attributeset
import android.util.log
import androidx.appcompat.widget.appcompattextview
import com.yw.custommutilimageadapter.r
import java.lang.stringbuilder
/**
 * 可改变字颜色和字体大小的textview
 * 即一个字体中可以有不同的字体颜色和字体大小
 */
class colorsizetextview(context: context, attrs: attributeset?) :
    appcompattextview(context, attrs) {
    init {
        val a = context.obtainstyledattributes(attrs, r.styleable.colorsizetextview)
        val textcolor1 = a.getcolor(r.styleable.colorsizetextview_textcolor1, color.black)
        val textsize1 = a.getdimension(r.styleable.colorsizetextview_textsize1, 12f)
        val textcontent1 = a.getstring(r.styleable.colorsizetextview_textcontent1)
        val textcolor2 = a.getcolor(r.styleable.colorsizetextview_textcolor2, color.black)
        val textsize2 = a.getdimension(r.styleable.colorsizetextview_textsize2, 12f)
        val textcontent2 = a.getstring(r.styleable.colorsizetextview_textcontent2)
        val textcolor3 = a.getcolor(r.styleable.colorsizetextview_textcolor3, color.black)
        val textsize3 = a.getdimension(r.styleable.colorsizetextview_textsize3, 12f)
        val textcontent3 = a.getstring(r.styleable.colorsizetextview_textcontent3)
        val islayout = a.getboolean(r.styleable.colorsizetextview_islayout, false)
        log.e("colorsizetextview:", "$textsize1,$textsize2,$textsize3")
        if (islayout) {
            // 创建一个spannablestring对象
            val spannablestring = spannablestring("$textcontent1$textcontent2$textcontent3")
            // 设置第一部分文本的字体大小和颜色
            spannablestring.setspan(
                absolutesizespan(textsize1.toint()),
                0,
                textcontent1?.length!!,
                spanned.span_exclusive_exclusive
            )
            spannablestring.setspan(
                foregroundcolorspan(textcolor1),
                0,
                textcontent1?.length!!,
                spanned.span_exclusive_exclusive
            )
            //设置第二部分文本的字体大小和颜色
            spannablestring.setspan(
                absolutesizespan(textsize2.toint()),
                textcontent1.length,
                textcontent1.length + textcontent2?.length!!,
                spanned.span_exclusive_exclusive
            )
            spannablestring.setspan(
                foregroundcolorspan(textcolor2),
                textcontent1.length,
                textcontent1.length + textcontent2.length,
                spanned.span_exclusive_exclusive
            )
            //设置第三部分文本的字体大小和颜色
            spannablestring.setspan(
                absolutesizespan(textsize3.toint()),
                textcontent1.length + textcontent2.length,
                spannablestring.length,
                spanned.span_exclusive_exclusive
            )
            spannablestring.setspan(
                foregroundcolorspan(textcolor3),
                textcontent1.length + textcontent2.length,
                spannablestring.length,
                spanned.span_exclusive_exclusive
            )
            text = spannablestring
            a.recycle()
        }
    }
    fun setsizecolordata(datas: arraylist<colorsizedata>) {
        val sbcontent = stringbuilder()
        datas.foreachindexed { index, colorsizedata ->
            sbcontent.append(colorsizedata.content)
        }
        // 创建一个spannablestring对象
        val spannablestring = spannablestring(sbcontent.tostring())
        var startindex = 0
        var endindex = 0
//        for (index in 0 until datas.size) {
//            endindex += datas[index].content?.length!!
//            if (index > 0) {
//                startindex += datas[index - 1].content?.length!!
//            }
//            log.e("colorsizetextview:", "$startindex,$endindex")
//        }
        for (index in 0 until datas.size) {
            endindex += datas[index].content?.length!!
            if (index > 0) {
                startindex += datas[index - 1].content?.length!!
            }
            log.e("colorsizetextview:", "$startindex,$endindex")
            log.e("colorsizetextview---size:", "${datas[index].textsize}")
            // 设置第一部分文本的字体大小和颜色
            spannablestring.setspan(
                absolutesizespan(datas[index].textsize),
                startindex,
                endindex,
                spanned.span_exclusive_exclusive
            )
            spannablestring.setspan(
                foregroundcolorspan(datas[index].textcolor),
                startindex,
                endindex,
                spanned.span_exclusive_exclusive
            )
        }
        text = spannablestring
    }
    class colorsizedata(
        var textcolor: int,
        var textsize: int,
        var content: string?
    )
}

2.用法

tvcontent.setsizecolordata(arraylist<colorsizetextview.colorsizedata>().apply {
            add(
                colorsizetextview.colorsizedata(
                    color.parsecolor("#3700b3"),
                    pxutils.sp2px(this@textviewdifferentcolorandsizeactivity,14f),
                    "德玛西亚啊"
                )
            )
            add(
                colorsizetextview.colorsizedata(
                    color.parsecolor("#ff7201"),
                    pxutils.sp2px(this@textviewdifferentcolorandsizeactivity,12f),
                    "诺克萨斯33333333333333333"
                )
            )
            add(
                colorsizetextview.colorsizedata(
                    color.parsecolor("#333333"),
                    pxutils.sp2px(this@textviewdifferentcolorandsizeactivity,10f),
                    "光辉女郎寒冰射手牛逼库拉斯"
                )
            )
        })

到此这篇关于android在一个textview中设置不同字体大小、不同字体颜色封装的文章就介绍到这了,更多相关android textview设置不同字体大小内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com