带图像示例的 SpannableString

发布于 2024-09-08 07:47:51 字数 87 浏览 4 评论 0原文

我正在寻找如何使用 ImageSpan 构建和显示 Android SpannableString 的示例。像笑脸的内联显示之类的东西。

多谢。

I am looking for an example of how to build and display Android SpannableString with ImageSpan. Something like inline display of smileys.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏雪无痕 2024-09-15 07:47:51

发现以下内容,它似乎可以完成工作:

public class TestActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    TextView textView  = (TextView) findViewById(R.id.textview); 
    SpannableString ss = new SpannableString("abc"); 
    Drawable d = ContextCompat.getDrawable(this, R.drawable.icon32);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
    ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
    textView.setText(ss); 
} 

Found the following and it seems to do the job:

public class TestActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    TextView textView  = (TextView) findViewById(R.id.textview); 
    SpannableString ss = new SpannableString("abc"); 
    Drawable d = ContextCompat.getDrawable(this, R.drawable.icon32);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
    ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
    textView.setText(ss); 
} 
你在我安 2024-09-15 07:47:51

SpannableString + ImageSpan 在 Android API 21 和 Android API 21 中不起作用22(我在模拟器中的 Android Studio 1.2.1.1 中进行了测试),但如果你这样做:

TextView textView  = (TextView) findViewById(R.id.textview);
textView.setTransformationMethod(null);
...
textView.setText(ss); 

SpannableString + ImageSpan 将起作用。

我受到这篇文章的启发:https://stackoverflow.com/a/26959656/3706042

SpannableString + ImageSpan don't work in Android API 21 & 22 (I tested in Android Studio 1.2.1.1 in emulator), but if you do this:

TextView textView  = (TextView) findViewById(R.id.textview);
textView.setTransformationMethod(null);
...
textView.setText(ss); 

SpannableString + ImageSpan will work.

I was inspired by this post: https://stackoverflow.com/a/26959656/3706042

夜深人未静 2024-09-15 07:47:51

如果有人仍然感兴趣,我创建了一个 Java 方法,允许基于“要替换的字符串”。

public void appendImages(@NonNull TextView textView,
                           @NonNull String text,
                           @NonNull String toReplace,
                           Drawable... drawables){
    if(drawables != null && drawables.length > 0){
        //list of matching positions, if any
        List<Integer> positions = new ArrayList<>();
        int index = text.indexOf(toReplace);
        while (index >= 0) {
            //add position
            positions.add(index);
            index = text.indexOf(toReplace, index + toReplace.length());
        }
        if(positions.size() > 0 && drawables.length >= positions.size()){
            textView.setTransformationMethod(null);
            SpannableString ss = new SpannableString(text);
            int drawablesIndex = 0;
            for(int position : positions){
                Drawable drawable = drawables[drawablesIndex++];
                //mandatory for Drawables
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                ss.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE), position, position+toReplace.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            textView.setText(ss);
        }
        else Timber.w("The amount of matches to replace is %s and the number of drawables to apply is %s", positions.size(), drawables.length);
    }
    else Timber.w("The drawables array is null or empty.");
}

用法:

appendImages(myTextView, "This is a ^ simple ^ test", "^", drawable1, drawable2);

If anyone is still interested, I've created the a Java method to allow adding recursively a listed drawables to a text (set at the end to a textView) based on a "string to replace".

public void appendImages(@NonNull TextView textView,
                           @NonNull String text,
                           @NonNull String toReplace,
                           Drawable... drawables){
    if(drawables != null && drawables.length > 0){
        //list of matching positions, if any
        List<Integer> positions = new ArrayList<>();
        int index = text.indexOf(toReplace);
        while (index >= 0) {
            //add position
            positions.add(index);
            index = text.indexOf(toReplace, index + toReplace.length());
        }
        if(positions.size() > 0 && drawables.length >= positions.size()){
            textView.setTransformationMethod(null);
            SpannableString ss = new SpannableString(text);
            int drawablesIndex = 0;
            for(int position : positions){
                Drawable drawable = drawables[drawablesIndex++];
                //mandatory for Drawables
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                ss.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE), position, position+toReplace.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            textView.setText(ss);
        }
        else Timber.w("The amount of matches to replace is %s and the number of drawables to apply is %s", positions.size(), drawables.length);
    }
    else Timber.w("The drawables array is null or empty.");
}

Usage:

appendImages(myTextView, "This is a ^ simple ^ test", "^", drawable1, drawable2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文