Android如何在一個(gè)TextView中設(shè)置不同字體大小、不同字體顏色封裝
一、概述
在開發(fā)過程中遇到過這樣一種業(yè)務(wù),有很多單行文本字體。字符串中每一部分的字體樣式、大小、顏色都不相同。傳統(tǒng)的做法是放多個(gè)TextView以達(dá)到效果。
但是當(dāng)這個(gè)頁面中的這樣的元素非常多,且非常復(fù)雜的時(shí)候,就會出現(xiàn)頁面加載緩慢的問題(view加載=深度(遞歸)+平鋪),也就是頁面元素越多,層級越深加載速度越慢。
此時(shí)如果能把本來要用四五個(gè)TextView才能完成的事情用一個(gè)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
* 即一個(gè)字體中可以有不同的字體顏色和字體大小
*/
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)建一個(gè)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)建一個(gè)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在一個(gè)TextView中設(shè)置不同字體大小、不同字體顏色封裝的文章就介紹到這了,更多相關(guān)android TextView設(shè)置不同字體大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- android 更改TextView中任意位置字體大小和顏色的方法
- Android 自定義TextView實(shí)現(xiàn)文本內(nèi)容自動(dòng)調(diào)整字體大小
- Android TextView 設(shè)置字體大小的方法
- Android編程實(shí)現(xiàn)自動(dòng)調(diào)整TextView字體大小以適應(yīng)文字長度的方法
- Android編程中TextView字體屬性設(shè)置方法(大小、字體、下劃線、背景色)
- Android 實(shí)現(xiàn)不同字體顏色的TextView實(shí)現(xiàn)代碼
- Android TextView設(shè)置不同的顏色字體
相關(guān)文章
Android中自定義ContentProvider實(shí)例
應(yīng)用A(TestBaidu)調(diào)用另外一個(gè)應(yīng)用(TestContentProvider)即自定義ContentProvider的使用,其它應(yīng)用調(diào)用該ContentProvider,具體如下,感興趣的朋友可以參考下哈2013-06-06
Android LayoutInflater.inflate()詳解及分析
這篇文章主要介紹了Android LayoutInflater.inflate()詳解及分析的相關(guān)資料,需要的朋友可以參考下2017-01-01
Flutter開發(fā)之Widget自定義總結(jié)
這篇文章主要給大家介紹了關(guān)于Flutter開發(fā)中Widget自定義的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android Studio+Servlet+MySql實(shí)現(xiàn)登錄注冊
對于大多數(shù)的APP都有登錄注冊這個(gè)功能,本文就來介紹一下Android Studio+Servlet+MySql實(shí)現(xiàn)登錄注冊,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Android開發(fā)之ViewFlipper自動(dòng)播放圖片功能實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android開發(fā)之ViewFlipper自動(dòng)播放圖片功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android使用ViewFlipper實(shí)現(xiàn)圖片播放的相關(guān)界面布局及功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03
Android使用OKHttp庫實(shí)現(xiàn)視頻文件的上傳到服務(wù)器功能
這篇文章主要介紹了Android使用OKHttp庫實(shí)現(xiàn)視頻文件的上傳到服務(wù)器功能,需要的朋友可以參考下2018-03-03
Android字體大小自適應(yīng)不同分辨率的解決辦法
這篇文章主要介紹了Android字體大小自適應(yīng)不同分辨率的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
Flutter質(zhì)感設(shè)計(jì)之進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

