欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android如何在一個TextView中設(shè)置不同字體大小、不同字體顏色封裝

 更新時間:2024年07月23日 10:14:08   作者:飄楊......  
在開發(fā)過程中遇到過這樣一種業(yè)務(wù),有很多單行文本字體,字符串中每一部分的字體樣式、大小、顏色都不相同,傳統(tǒng)的做法是放多個TextView以達到效果,這篇文章主要介紹了android在一個TextView中設(shè)置不同字體大小、不同字體顏色封裝,需要的朋友可以參考下

一、概述

  在開發(fā)過程中遇到過這樣一種業(yè)務(wù),有很多單行文本字體。字符串中每一部分的字體樣式、大小、顏色都不相同。傳統(tǒng)的做法是放多個TextView以達到效果。

  但是當(dāng)這個頁面中的這樣的元素非常多,且非常復(fù)雜的時候,就會出現(xiàn)頁面加載緩慢的問題(view加載=深度(遞歸)+平鋪),也就是頁面元素越多,層級越深加載速度越慢。

  此時如果能把本來要用四五個TextView才能完成的事情用一個TextView完成,這樣就能大大提高頁面加載速度。

  示例圖:

二、代碼示例(直接復(fù)制粘貼可用)

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) {
            // 創(chuàng)建一個SpannableString對象
            val spannableString = SpannableString("$textContent1$textContent2$textContent3")
            // 設(shè)置第一部分文本的字體大小和顏色
            spannableString.setSpan(
                AbsoluteSizeSpan(textSize1.toInt()),
                0,
                textContent1?.length!!,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            spannableString.setSpan(
                ForegroundColorSpan(textColor1),
                0,
                textContent1?.length!!,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            //設(shè)置第二部分文本的字體大小和顏色
            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
            )
            //設(shè)置第三部分文本的字體大小和顏色
            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)
        }
        // 創(chuàng)建一個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}")
            // 設(shè)置第一部分文本的字體大小和顏色
            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),
                    "光輝女郎寒冰射手牛逼庫拉斯"
                )
            )
        })

到此這篇關(guān)于android在一個TextView中設(shè)置不同字體大小、不同字體顏色封裝的文章就介紹到這了,更多相關(guān)android TextView設(shè)置不同字體大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論