Java 单一文本视图中的多字体

Java 单一文本视图中的多字体,java,android,Java,Android,我想将TextView上的第一个字符设置为字体,将第二个字符设置为不同的字体,依此类推。 我读了这个例子: Spannable str = (Spannable) textView.getText(); str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7 ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 但这对我没有帮助,因为我

我想将
TextView
上的第一个字符设置为
字体
,将第二个字符设置为不同的字体,依此类推。
我读了这个例子:

Spannable str = (Spannable) textView.getText();
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7  
                             ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
但这对我没有帮助,因为我想设置多个
字体(外部TTF)

有什么想法吗???

使用以下代码:(我使用的是孟加拉语和泰米尔语字体)
结果是:


这使用了
customTypeface span
类,取自:



Kotlin开发者可以使用它为单个按钮或文本视图使用两种或两种以上的字体

1。复制并粘贴此类:

class CustomTypefaceSpan(family: String?, private val newType: Typeface) : TypefaceSpan(family) {

    override fun updateDrawState(ds: TextPaint) {
        applyCustomTypeFace(ds, newType)
    }

    override fun updateMeasureState(paint: TextPaint) {
        applyCustomTypeFace(paint, newType)
    }

    companion object {
        private fun applyCustomTypeFace(paint: Paint, tf: Typeface) {
            val oldStyle: Int
            val old = paint.typeface
            oldStyle = old?.style ?: 0
            val fake = oldStyle and tf.style.inv()
            if (fake and Typeface.BOLD != 0) {
                paint.isFakeBoldText = true
            }
            if (fake and Typeface.ITALIC != 0) {
                paint.textSkewX = -0.25f
            }
            paint.typeface = tf
        }
    }
}
val buttonText = "Get it for 100 Points"

//making text yellow
    buttonText.setSpan(
        ForegroundColorSpan(
            ContextCompat.getColor(
                this, R.color.yellow_FFC001
            )
        ),
        11, 14,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )

//changing text size of yellow portion i.e. "100"
    val sp20 = resources.getDimensionPixelSize(R.dimen.sp_20)
    buttonText.setSpan(AbsoluteSizeSpan(sp20), 11, 14, Spannable.SPAN_INCLUSIVE_INCLUSIVE)

//setting button fonts
    buttonText.setSpan(
        CustomTypefaceSpan("", ResourcesCompat.getFont(this, R.font.roboto_condensed_bold)!!),
        0,
        buttonText.length,
        0
    )

//finally setting this text to my button
btGetPoints.text = buttonText
2。使用此选项的人:

class CustomTypefaceSpan(family: String?, private val newType: Typeface) : TypefaceSpan(family) {

    override fun updateDrawState(ds: TextPaint) {
        applyCustomTypeFace(ds, newType)
    }

    override fun updateMeasureState(paint: TextPaint) {
        applyCustomTypeFace(paint, newType)
    }

    companion object {
        private fun applyCustomTypeFace(paint: Paint, tf: Typeface) {
            val oldStyle: Int
            val old = paint.typeface
            oldStyle = old?.style ?: 0
            val fake = oldStyle and tf.style.inv()
            if (fake and Typeface.BOLD != 0) {
                paint.isFakeBoldText = true
            }
            if (fake and Typeface.ITALIC != 0) {
                paint.textSkewX = -0.25f
            }
            paint.typeface = tf
        }
    }
}
val buttonText = "Get it for 100 Points"

//making text yellow
    buttonText.setSpan(
        ForegroundColorSpan(
            ContextCompat.getColor(
                this, R.color.yellow_FFC001
            )
        ),
        11, 14,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )

//changing text size of yellow portion i.e. "100"
    val sp20 = resources.getDimensionPixelSize(R.dimen.sp_20)
    buttonText.setSpan(AbsoluteSizeSpan(sp20), 11, 14, Spannable.SPAN_INCLUSIVE_INCLUSIVE)

//setting button fonts
    buttonText.setSpan(
        CustomTypefaceSpan("", ResourcesCompat.getFont(this, R.font.roboto_condensed_bold)!!),
        0,
        buttonText.length,
        0
    )

//finally setting this text to my button
btGetPoints.text = buttonText
3。最终输出结果如下所示


假设您有两种字体,如
typeface font=typeface.createfromsete(getAssets(),“1.ttf”);Typeface font2=Typeface.createFromAsset(getAssets(),“2.ttf”)然后,
SpannableStringBuilder SS=new SpannableStringBuilder(“您的文本”);SS.setSpan(新样式SPAN(font2.getStyle()),0,2,跨距。SPAN_独占_包括在内);SS.setSpan(新样式SPAN(font.getStyle())、3、6、SPAN.SPAN(包含此项)。你尝试过这种方法吗?我尝试过,但没有成功,我想font1.getStyle()获得字体的样式,而不是字体本身。我找到了解决问题的方法,看看我的答案:)。如果布局中有
android:textAllCaps=“true”
,则值得指定此代码无效。@Imran谢谢。自豪的是你是孟加拉人