Android 带有图像示例的 SpannableString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3176033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:02:44  来源:igfitidea点击:

SpannableString with Image example

androidspannableimagespan

提问by Asahi

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

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

Thanks a lot.

非常感谢。

回答by Asahi

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

回答by Dedkov Vadim

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:

SpannableString + ImageSpan 在 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 will work.

SpannableString + ImageSpan 将起作用。

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

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