gpt4 book ai didi

android - spannableStringBuilder 无法在单个 TextView 中使用多个跨度

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:11 25 4
gpt4 key购买 nike

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

    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());

enter image description here

有人请帮我弄清楚这里面有什么问题,或者它是可跨字符串/ 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;

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

最佳答案

我只是稍微更改了您的 Utility 类,请参见下文:

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 也做同样的事情,那么它也可以正常工作。试试这个,让我们知道......祝你好运

关于android - spannableStringBuilder 无法在单个 TextView 中使用多个跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41583604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com