我想在自定义TextView的段落末尾放置一个小的动画图像,但当我使用以下代码时,它不会显示图像:
cmptDiag.setSpan(
ImageSpan(context, R.drawable.diagpause_anim_gif, DynamicDrawableSpan.ALIGN_BASELINE),
cmptDiag.length-2,
cmptDiag.length-1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)我怀疑'context‘会有更好的选择,但找不到有效的东西。getContext()不起作用,我不能把'this‘和'this@theCustomTextView’放在一起。
发布于 2020-10-29 15:15:52
请查看此示例。
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
}
}此示例将以文本形式显示图像。
如果您想添加bound,请选中此。
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);这个'setBound‘示例是由java编写的。但我想你可以换成kotlin
https://stackoverflow.com/questions/64568970
复制相似问题