Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ImageSpan不显示图像_Android_Kotlin - Fatal编程技术网

Android ImageSpan不显示图像

Android ImageSpan不显示图像,android,kotlin,Android,Kotlin,我想在自定义文本视图的段落末尾放置一个小的动画图像,但当我使用以下代码时,它不会显示图像: cmptDiag.setSpan( ImageSpan(context, R.drawable.diagpause_anim_gif, DynamicDrawableSpan.ALIGN_BASELINE), cmptDiag.length-2, cmptDiag.length-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) 我怀疑“上下文”会有更好的选择,

我想在自定义文本视图的段落末尾放置一个小的动画图像,但当我使用以下代码时,它不会显示图像:

cmptDiag.setSpan(
  ImageSpan(context, R.drawable.diagpause_anim_gif, DynamicDrawableSpan.ALIGN_BASELINE),
  cmptDiag.length-2,
  cmptDiag.length-1,
  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
我怀疑“上下文”会有更好的选择,但找不到适合它的东西。getContext()不起作用,我不能将'this'或'this@theCustomTextView”。

请检查此示例

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val text = ("Lorem Ipsum is simply dummy text of the printing   and" +
                " typesetting industry. Lorem Ipsum   has been the industry's" +
                " standard dummy text ever since the 1500s, when an unknown" +
                " printer took a galley   of type and scrambled it" +
                " to make a type specimen book.").toSpannable()

        // Get icon from drawable resource
        var icon:Bitmap = BitmapFactory.decodeResource(resources,R.drawable.cfsuman)

        // Scale bitmap using android kotlin core ktx function
        icon = icon.scale(100,100,false)

        // Image span from drawable icon
        text[49..50] = ImageSpan(this,R.drawable.ic_weekend)

        // Another image span from bitmap
        text[89..90] = ImageSpan(this,icon)

        textView.text = text
    }
}
本例将以文本形式显示图像

如果要添加绑定,请选中此项

Drawable image = ContextCompat.getDrawable(mContext, android.R.drawable.presence_offline);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
String myText = "myText";
int textLength = myText.length();
SpannableString sb = new SpannableString(myText + "   " + "This is another text");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, textLength, textLength + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
这个由java编写的“setBound”示例。但是我想你可以改成kotlin查看这个例子

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val text = ("Lorem Ipsum is simply dummy text of the printing   and" +
                " typesetting industry. Lorem Ipsum   has been the industry's" +
                " standard dummy text ever since the 1500s, when an unknown" +
                " printer took a galley   of type and scrambled it" +
                " to make a type specimen book.").toSpannable()

        // Get icon from drawable resource
        var icon:Bitmap = BitmapFactory.decodeResource(resources,R.drawable.cfsuman)

        // Scale bitmap using android kotlin core ktx function
        icon = icon.scale(100,100,false)

        // Image span from drawable icon
        text[49..50] = ImageSpan(this,R.drawable.ic_weekend)

        // Another image span from bitmap
        text[89..90] = ImageSpan(this,icon)

        textView.text = text
    }
}
本例将以文本形式显示图像

如果要添加绑定,请选中此项

Drawable image = ContextCompat.getDrawable(mContext, android.R.drawable.presence_offline);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
// Replace blank spaces with image icon
String myText = "myText";
int textLength = myText.length();
SpannableString sb = new SpannableString(myText + "   " + "This is another text");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, textLength, textLength + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

这个由java编写的“setBound”示例。但是我想你可以换成kotlin,你必须在你的绘图栏中加一点提示,好吗?我在下拉菜单中找不到它。似乎您正在使用SPAN_EXCLUSIVE_EXCLUSIVE,它将同时排除起始字符和结束字符。我认为如果长度为0,跨度会自动从文本中删除。您可以尝试将其更改为SPAN_INCLUSIVE_INCLUSIVE,或更改此2
cmptDiag.length-2、cmptDiag.length-1、
以确保SPAN长度不是0。您必须在绘图中设置边界。请再提示一点?我在下拉菜单中找不到它。似乎您正在使用SPAN_EXCLUSIVE_EXCLUSIVE,它将同时排除起始字符和结束字符。我认为如果长度为0,跨度会自动从文本中删除。您可以尝试将其更改为SPAN_INCLUSIVE_INCLUSIVE或更改此2
cmptDiag.length-2、cmptDiag.length-1、
,以确保SPAN长度不是0。感谢您的帮助!在自定义视图中,这些代码(包括我的代码)似乎不像在活动中那样工作。我可以安排他们参加各种活动。我想我需要晚一点再研究一下。谢谢你的帮助!在自定义视图中,这些代码(包括我的代码)似乎不像在活动中那样工作。我可以安排他们参加各种活动。我想我需要晚一点再研究。。