gpt4 book ai didi

android - 如何在 TextView 上应用图像和文本跨度

转载 作者:行者123 更新时间:2023-11-29 01:33:10 25 4
gpt4 key购买 nike

我想在 TextView 的文本上应用 span 有两个目的。首先是解析笑脸,其次是突出显示文本。我出于同样的目的创建了这个方法,但没有用。请告诉我哪里出错了。

public CharSequence addSmileySpansTextFilter(final CharSequence text,String filter,int color) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);

Matcher matcher = mPattern.matcher(text);
while (matcher.find()) {
int resId = mSmileyToRes.get(matcher.group());
builder.setSpan(new ImageSpan(mContext, resId), matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


}

if (filter!=null&&!filter.equals("")) {

int startPos = builder.toString().toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US));
int endPos = startPos + filter.length();


if (startPos != -1) // This should always be true, just a sanity check
{

BackgroundColorSpan highlightSpan = new BackgroundColorSpan(color);

builder.setSpan(highlightSpan, startPos, endPos,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

}
}
return builder;
}

最佳答案

试试这个 .. TextViewWithImages 的 Utils 类

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.Spannable;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class TextViewWithImages extends TextView {

public TextViewWithImages(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TextViewWithImages(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewWithImages(Context context) {
super(context);
}
@Override
public void setText(CharSequence text, BufferType type) {
Spannable s = getTextWithImages(getContext(), text);
super.setText(s, BufferType.SPANNABLE);
}

private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();

private static boolean addImages(Context context, Spannable spannable) {
Pattern refImg = Pattern.compile("\\Q[img src=\\E([a-zA-Z0-9_]+?)\\Q/]\\E");
boolean hasChanges = false;

Matcher matcher = refImg.matcher(spannable);
while (matcher.find()) {
boolean set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) {
if (spannable.getSpanStart(span) >= matcher.start()
&& spannable.getSpanEnd(span) <= matcher.end()
) {
spannable.removeSpan(span);
} else {
set = false;
break;
}
}
String resname = spannable.subSequence(matcher.start(1), matcher.end(1)).toString().trim();
int id = context.getResources().getIdentifier(resname, "drawable", context.getPackageName());
if (set) {
hasChanges = true;
spannable.setSpan( new ImageSpan(context, id),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
}

return hasChanges;
}
private static Spannable getTextWithImages(Context context, CharSequence text) {
Spannable spannable = spannableFactory.newSpannable(text);
addImages(context, spannable);
return spannable;
}
}

使用:

在 res/layout/mylayout.xml 中:

<com.xyz.customandroid.TextViewWithImages
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF00"
android:text=""
android:textSize="12dip"
style=...
/>

请注意,如果您将 TextViewWithImages.java 放在 com/xyz/customandroid/以外的某个位置,您还必须更改包名称,上面的 com.xyz.customandroid。

关于android - 如何在 TextView 上应用图像和文本跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30124048/

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