小编use*_*038的帖子

带有SpannableStringBuilder和ImageSpan的Wrappable TextView隐藏了每一行的最后一个图像

我正在用元素动态填充多行TextView.输入的元素行是可以包装的.每个元素由两部分组成:文本后跟图像.图像的长度为2个字符.元素由空格分隔.添加元素我正在使用SpannableStringBuilder.除了一件事,一切都很好.在添加到下一行的新元素(TextView包装线)时,前一行元素的图像(即上一行中的最后一个元素)将消失,无论该行上仍有多少空间可用.如果我删除新行上新添加的元素,该图像将再次出现.因此,每行上最后一个元素的图像部分不会显示出来.我使用的是Android 4.0.3.

这是我的代码:

TextView textView = (TextView) findViewById(R.id.textview);
textView.setMovementMethod(new ScrollingMovementMethod());

SpannableStringBuilder ssb = new SpannableStringBuilder();

//then for each new element I do the following
ssb.append(" "); //space separating elements (skip for the first element)
ssb.append("text");
Bitmap image = BitmapFactory.decodeResource( getResources(), imageId );
ssb.append("  "); //prepare 2-character space for the image
ssb.setSpan( new ImageSpan(image), ssb.length() - 2, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); //put the image to the prepared space
textView.setText( ssb, BufferType.SPANNABLE );
Run Code Online (Sandbox Code Playgroud)

我知道TextWatcher有一个解决方案.同时,我在代码中看不到任何流,也无法理解为什么每行上的最后一个图像都被隐藏了.任何建议将受到高度赞赏.谢谢.

java android textview

3
推荐指数
1
解决办法
1300
查看次数

标签 统计

android ×1

java ×1

textview ×1