带有 ImageSpan 的 TextView 弄乱了行高 [英] TextView with ImageSpan messes up line height
问题描述
我有一个 TextView
填充的文本,应该包含一些 ImageSpan
对象.图像可能比正常的行高高,这会导致以下问题:
I have a TextView
filled with text that should contain some ImageSpan
objects. The images can be taller than the normal line height which causes the following problem:
- 如果图像是一行的最后一个对象,则以下行的高度是正确的
- 如果最后一个对象不是图像,则以下行的高度设置为包含图像的行的高度
- if the image is the last object of a line, the following lines' height is correct
- if the last object is not an image, the following lines' height is set to the image-containing line's height
以下是正确的情况:
这是错误的情况:
更有趣的是,如果文本中有换行符,那么从那一点开始行高就又好了.
What's more interesting is that if there's a new-line character in the text, the line height is good from that point on again.
TextView
只是一个非常基本的:
The TextView
is just a pretty basic one:
<TextView
android:id="@+id/text_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="18dp"
android:text="Text 02" />
(TextView
位于 LinearLayout
中,ScrollView
中.)
(The TextView
is located in a LinearLayout
which is in a ScrollView
.)
这就是我创建跨区文本的方式:
This is how I create the spanned text:
TextView textView02 = (TextView) findViewById(R.id.text_02);
SpannableString string = new SpannableString(LOREM_IPSUM);
string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 102, 103, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 105, 106, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 108, 109, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView02.setText(string);
有没有人知道这个问题的解决方案?我宁愿不重新实现 TextView
的线条绘制方法...
Does anybody have any idea about a solution for this? I'd rather not reimplement the TextView
's line drawing methods...
推荐答案
尝试为要使用 ImageSpan
显示的可绘制对象设置高度.例如像这样:
Try to set a height to the drawable you want to show with the ImageSpan
. For example like this:
Drawable vegetary = mContext.getResources().getDrawable(R.drawable.ic_best_veget);
vegetary.setBounds(0, 0, vegetary.getIntrinsicWidth(), <yourHeight>);
ssb.setSpan(new ImageSpan(vegetary, ImageSpan.ALIGN_BASELINE), ssb.length()-1, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
这篇关于带有 ImageSpan 的 TextView 弄乱了行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!