use*_*038 3 java android 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有一个解决方案.同时,我在代码中看不到任何流,也无法理解为什么每行上的最后一个图像都被隐藏了.任何建议将受到高度赞赏.谢谢.
我找到了解决方案.问题在于计算文本行.如果一行以空格字符结尾,则在计算期间将忽略此空格.因此,您所要做的就是使用任何字母作为图像的占位符.像这样:
ssb.append(" i"); //prepare 2-characters for the image
Run Code Online (Sandbox Code Playgroud)
PS:我不知道为什么你为每个图像使用2个字符串,但我不得不说你只能使用一个字符.这是一个更有效的代码:
int start = ssb.length();
ssb.append('i');
ssb.setSpan(new ImageSpan(image), start, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1300 次 |
| 最近记录: |