使用ImageSpan的TextView会破坏行高

时间:2016-09-27 15:45:14

标签: android textview spannablestring imagespan

我的TextView填充了应包含一些ImageSpan个对象的文本。图像可能高于法线高度,导致以下问题:

  • 如果图像是一行的最后一个对象,则以下行的高度是正确的
  • 如果最后一个对象图像,则以下行的高度设置为包含图像的行的高度

以下是正确的情况: enter image description here

这是错误的情况: enter image description here

更有趣的是,如果文本中有换行符,那么从那一点开始,行高就会很好。

TextView只是一个非常基本的问题:

<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

这就是我创建跨区文本的方式:

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的线条绘制方法......

1 个答案:

答案 0 :(得分:0)

尝试使用ImageSpan设置要显示的drawable的高度。例如:

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);