spannableStringBuilder 无法在单个文本视图中使用多个跨度

spannableStringBuilder not working with multiple span in single text view

下面是一个实用程序class,用于制作具有所需格式的可跨越字符串

    package impressico.com.testfragmentstack;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import java.util.ArrayList;
import java.util.List;

public class SimpleSpanBuilder {
  public static final int FORMATTING_STYLE_DARK_BOLD = 1;
  public static final int FORMATTING_STYLE_DARK_BOLD_SMALL = 2;
  public static final int FORMATTING_STYLE_DIM_ITALIC_LIGHT = 3;
  public static final int FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL = 4;
  private List<SpanSection> spanSections;
  private StringBuilder stringBuilder;
  ForegroundColorSpan boldColorSpan;
  ForegroundColorSpan dimColorSpan;
  ForegroundColorSpan testColorSpan1;
  ForegroundColorSpan testColorSpan2;
  RelativeSizeSpan relativeSmallSpan;

  public SimpleSpanBuilder(Context context) {
    stringBuilder = new StringBuilder();
    spanSections = new ArrayList<>();
    boldColorSpan =
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Green));
    dimColorSpan =
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Blue));
    testColorSpan1 =
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Black));
    testColorSpan2 =
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Red));
    relativeSmallSpan = new RelativeSizeSpan(0.8f);
  }

  public SimpleSpanBuilder append(String text, int formattingStyle) {
    spanSections.add(new SpanSection(text, stringBuilder.length(), formattingStyle));
    stringBuilder.append(text);
    return this;
  }

  public SpannableStringBuilder build() {
    SpannableStringBuilder ssb = new SpannableStringBuilder(stringBuilder.toString());
    for (SpanSection section : spanSections) {
      section.apply(ssb);
    }
    return ssb;
  }

  @Override
  public String toString() {
    return stringBuilder.toString();
  }

  private class SpanSection {
    private final String text;
    private final int startIndex;
    private final int formattingStyle;

    public SpanSection(String text, int startIndex, int formattingStyle) {
      this.formattingStyle = formattingStyle;
      this.text = text;
      this.startIndex = startIndex;
    }

    public void apply(SpannableStringBuilder spanStringBuilder) {
      if (spanStringBuilder == null) return;
      switch (formattingStyle) {
        case FORMATTING_STYLE_DARK_BOLD:
          spanStringBuilder.setSpan(boldColorSpan, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          break;
        case FORMATTING_STYLE_DARK_BOLD_SMALL:
          spanStringBuilder.setSpan(testColorSpan1, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          break;
        case FORMATTING_STYLE_DIM_ITALIC_LIGHT:
          spanStringBuilder.setSpan(dimColorSpan, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          break;
        case FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL:
          spanStringBuilder.setSpan(testColorSpan2, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          break;
      }
    }
  }
}

当我尝试使用下面的调用代码在 span 上应用格式时,它没有按预期工作

    tv=((TextView) view.findViewById(R.id.fragment_title));
    SimpleSpanBuilder ssbTest=new SimpleSpanBuilder(getContext());
    ssbTest.append("Green",1);
    ssbTest.append("Black",2);
    ssbTest.append("Blue",3);
    ssbTest.append("Red",4);
    ssbTest.append("Green",1);
    ssbTest.append("Black",2);
    ssbTest.append("Blue",3);
    ssbTest.append("Red",4);
    tv.setText(ssbTest.build());

有人请帮我弄清楚这有什么问题或者它是 spannable 的错误 string/textview?

更新 感谢@TdSoft 的解决方案,@W.K.S 这是扩展代码的原因

case FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL: {
          CalligraphyTypefaceSpan typefaceSemiBoldItalic = new CalligraphyTypefaceSpan(typefaceSBI);
          ForegroundColorSpan dimColorSpan =
              new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_color_ffa7acb3));
          RelativeSizeSpan relativeSmallSpan = new RelativeSizeSpan(0.8f);
          spanStringBuilder.setSpan(typefaceSemiBoldItalic, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          spanStringBuilder.setSpan(dimColorSpan, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          spanStringBuilder.setSpan(relativeSmallSpan, startIndex, startIndex + text.length(),
              Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        break;

我需要一套固定的格式样式,上面的代码在很多地方都被调用,所以最好向调用者公开格式代码。

我只是稍微改变了你的实用程序 class 请看下面:

public class SimpleSpanBuilder {
    public static final int FORMATTING_STYLE_DARK_BOLD = 1;
    public static final int FORMATTING_STYLE_DARK_BOLD_SMALL = 2;
    public static final int FORMATTING_STYLE_DIM_ITALIC_LIGHT = 3;
    public static final int FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL = 4;
    private List<SpanSection> spanSections;
    private StringBuilder stringBuilder;
    RelativeSizeSpan relativeSmallSpan;
    private Context context;
    public SimpleSpanBuilder(Context context) {
        this.context = context;
        stringBuilder = new StringBuilder();
        spanSections = new ArrayList<>();
        relativeSmallSpan = new RelativeSizeSpan(0.8f);
    }

    public SimpleSpanBuilder append(String text, int formattingStyle) {
        spanSections.add(new SpanSection(text, stringBuilder.length(), formattingStyle));
        stringBuilder.append(text);
        return this;
    }

    public SpannableStringBuilder build() {
        SpannableStringBuilder ssb = new SpannableStringBuilder(stringBuilder.toString());
        for (SpanSection section : spanSections) {
            section.apply(ssb);
        }
        return ssb;
    }

    @Override
    public String toString() {
        return stringBuilder.toString();
    }

    private class SpanSection {
        private final String text;
        private final int startIndex;
        private final int formattingStyle;

        public SpanSection(String text, int startIndex, int formattingStyle) {
            this.formattingStyle = formattingStyle;
            this.text = text;
            this.startIndex = startIndex;
        }

        public void apply(SpannableStringBuilder spanStringBuilder) {
            if (spanStringBuilder == null) return;
            switch (formattingStyle) {
                case FORMATTING_STYLE_DARK_BOLD:
                    ForegroundColorSpan boldColorSpan =
                            new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Green));
                    spanStringBuilder.setSpan(boldColorSpan, startIndex, startIndex + text.length(),
                            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    break;
                case FORMATTING_STYLE_DARK_BOLD_SMALL:
                    ForegroundColorSpan testColorSpan1 =
                            new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Black));

                    spanStringBuilder.setSpan(testColorSpan1, startIndex, startIndex + text.length(),
                            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    break;
                case FORMATTING_STYLE_DIM_ITALIC_LIGHT:
                    ForegroundColorSpan dimColorSpan =
                            new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Blue));
                    spanStringBuilder.setSpan(dimColorSpan, startIndex, startIndex + text.length(),
                            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    break;
                case FORMATTING_STYLE_DIM_ITALIC_LIGHT_SMALL:
                    ForegroundColorSpan testColorSpan2 =
                            new ForegroundColorSpan(ContextCompat.getColor(context, R.color.Red));
                    spanStringBuilder.setSpan(testColorSpan2, startIndex, startIndex + text.length(),
                            Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    break;
            }
        }
    }
}

注意:对于relativeSmallSpan也做同样的事情,那么它也可以正常工作。 试试这个,让我们知道...... 运气好

我在 this 答案中写了 SimpleSpanBuilder class。您已经扩展了 class 并使其不必要地复杂化。很简单:

SimpleSpanBuilder ssbTest = new SimpleSpanBuilder();

ssbTest.append("Green",new ForegroundColorSpan(Color.GREEN));
ssbTest.append("Black",new ForegroundColorSpan(Color.BLACK));
ssbTest.append("Blue",new ForegroundColorSpan(Color.BLUE));
ssbTest.append("Red",new ForegroundColorSpan(Color.RED));

ssbTest.append("Green",new ForegroundColorSpan(Color.GREEN));
ssbTest.append("Black",new ForegroundColorSpan(Color.BLACK));
ssbTest.append("Blue",new ForegroundColorSpan(Color.BLUE));
ssbTest.append("Red",new ForegroundColorSpan(Color.RED));


textView.setText(ssbTest.build());

更新

我还没有测试过这段代码,但是要声明一个命名样式,您可以这样做:

public static class SpanStyleSheet{

    private static ParcelableSpan[] dimItalicLightSmall;

    public static ParcelableSpan[] dimItalicLightSmall(Context context){

        if(dimItalicLightSmall == null){
            dimItalicLightSmall =  new ParcellableSpan[]{
                new CalligraphyTypefaceSpan(typefaceSBI),
                new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_color_ffa7acb3)),
                new RelativeSizeSpan(0.8f)
            }
        }
        return dimItalicLightSmall;
    }
}

SimpleSpanBuilder ssbTest = new SimpleSpanBuilder();
ssbTest.append("Green",SpanStyleSheet.dimItalicLightSmall(getContext()));
textView.setText(ssbTest.build());