android 富文本

封装一个工具类

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import com.example.jing.maya_android.application.KapApplication;

/**
 * Created by jing on 2017/6/16.
 * 富文本生成器
 */
public class KapAttributedStringHelper {
    /** 改变选中字体的颜色 */
    public static SpannableStringBuilder AttributedStringByConfi(String allString
            , int normalColor, int selectedColor, String[] selectedStrings){
        if (allString.length() == 0) return null;
        SpannableStringBuilder sb = new SpannableStringBuilder(allString);
        ForegroundColorSpan normalColorSpan = new ForegroundColorSpan(KapApplication.getContext().getResources().getColor(normalColor));
        ForegroundColorSpan selectedColorSpan = new ForegroundColorSpan(KapApplication.getContext().getResources().getColor(selectedColor));
        sb.setSpan(normalColorSpan, 0, allString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        for(int i=0;i

使用如下:

 {
            String showString = String.format(priceshow_price_format,minPrice);
            String[] selectString = {minPrice};
            int[] selectSize = {KapDbPxTransHelper.sp2px(this,16)};
            SpannableStringBuilder priceSpan = new KapAttributedStringHelper(this,showString,selectString)
                    .setNormalSize(KapDbPxTransHelper.sp2px(this,12))
                    .setSelectlSize(selectSize)
                    .builder();
            priceTextview.setText(priceSpan);
        }
        {
            String showString = String.format(priceshow_price_sub_prompt,minPrice,maxPrice);
            String[] selectString = {minPrice,maxPrice};
            int[] selectColor = {R.color.BANK_GRAY_525252,R.color.Bank_Yellow_ff9100};
            SpannableStringBuilder priceSpan = new KapAttributedStringHelper(this,showString,selectString)
                    .setNormalColor(R.color.BANK_GRAY_bfbfbf)
                    .setSelectlColor(selectColor)
                    .builder();
            priceSubPromptTextview.setText(priceSpan);
        }

你可能感兴趣的:(android 富文本)