[英]How can I get rid of unwanted space over ImageSpan that appears if I also add text to EditText?
How can I get rid of unwanted space over ImageSpan that appears if I add text to EditText?如果将文本添加到 EditText,如何消除出现的 ImageSpan 上不需要的空间?
In EditText
, a single ImageSpan
takes the whole height, but if I add any text, unwanted space appears over the image, and height of the EditText
changes.在
EditText
,单个ImageSpan
占据整个高度,但如果我添加任何文本,图像上会出现不需要的空间,并且EditText
高度会发生变化。 Can I avoid that?我可以避免吗?
It appears that the height of the EditText is calculated as the difference between the minimum top of the text and the maximum bottom.似乎 EditText 的高度计算为文本的最小顶部和最大底部之间的差异。 Direction in the font metrics is downwards, and 0 is the base line of the text.
字体度量中的方向是向下的,0 是文本的基线。
In the left screenshot, as set by DynamicDrawableSpan.getSize()
, bottom is 0, and top is -250, which is the image height.在左侧截图中,由
DynamicDrawableSpan.getSize()
设置,底部为 0,顶部为 -250,即图像高度。 That gives us the height of 250.这给了我们 250 的高度。
In the right screenshot, the additional text sets the bottom to 15. Top is still at -250.在右侧的屏幕截图中,附加文本将底部设置为 15。顶部仍为 -250。 The height becomes 265 pixels.
高度变为 265 像素。
ImageSpan
still draws from the bottom, so you get this weird space. ImageSpan
仍然从底部绘制,所以你会得到这个奇怪的空间。
I'm not sure if this is the best solution but this works for my use case:我不确定这是否是最佳解决方案,但这适用于我的用例:
class FullHeightImageSpan(
val context: Context,
val bitmap: Bitmap
) : ImageSpan(context, bitmap) {
override fun getSize(paint: Paint, text: CharSequence?,
start: Int, end: Int,
fm: Paint.FontMetricsInt?): Int {
val oldBottom = fm?.bottom
val result = super.getSize(paint, text, start, end, fm)
fm?.apply {
top += oldBottom!!
ascent = top
bottom = oldBottom
}
return result
}
}
Here I'm simply preserving the bottom
of the text instead of setting it to 0.在这里,我只是保留了文本的
bottom
,而不是将其设置为 0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.