同一个TextView如何显示不同颜色的文字、字体大小、字体样式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34581102/article/details/78416370
     * 改变文字大小和字体样式
     *
     * @param str
     * @param tv
     */
    public void changeTextSize(String str, TextView tv) {
        Spannable WordtoSpan = new SpannableString(str);
        WordtoSpan.setSpan(new AbsoluteSizeSpan(20, true), 0, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);设置字体大小
        WordtoSpan.setSpan(new AbsoluteSizeSpan(10, true), str.length() - 1, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//设置字体大小
        WordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 设置字体样式
        tv.setText(WordtoSpan);
    }
    /**
     * 改变文字颜色
     * @param str
     * @param tv
     */
    public void changeTextColor(String str,TextView tv){
        SpannableString spannableString = new SpannableString(str);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_main_line)); // 设置字体颜
        spannableString.setSpan(colorSpan, 2, spannableString.length()-4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv.setText(spannableString);
    }

猜你喜欢

转载自blog.csdn.net/qq_34581102/article/details/78416370