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)文章
Android LayoutInflater.inflate()詳解及分析
這篇文章主要介紹了Android LayoutInflater.inflate()詳解及分析的相關(guān)資料,需要的朋友可以參考下2017-01-01Flutter開發(fā)之Widget自定義總結(jié)
這篇文章主要給大家介紹了關(guān)于Flutter開發(fā)中Widget自定義的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Android Studio+Servlet+MySql實現(xiàn)登錄注冊
對于大多數(shù)的APP都有登錄注冊這個功能,本文就來介紹一下Android Studio+Servlet+MySql實現(xiàn)登錄注冊,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Android開發(fā)之ViewFlipper自動播放圖片功能實現(xiàn)方法示例
這篇文章主要介紹了Android開發(fā)之ViewFlipper自動播放圖片功能實現(xiàn)方法,結(jié)合實例形式分析了Android使用ViewFlipper實現(xiàn)圖片播放的相關(guān)界面布局及功能實現(xiàn)技巧,需要的朋友可以參考下2019-03-03Android使用OKHttp庫實現(xiàn)視頻文件的上傳到服務(wù)器功能
這篇文章主要介紹了Android使用OKHttp庫實現(xiàn)視頻文件的上傳到服務(wù)器功能,需要的朋友可以參考下2018-03-03Android字體大小自適應(yīng)不同分辨率的解決辦法
這篇文章主要介紹了Android字體大小自適應(yīng)不同分辨率的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06