简体   繁体   English

如果我还向 EditText 添加文本,如何消除出现的 ImageSpan 上不需要的空间?

[英]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.

相关问题 使用SpannableStringBuilder添加图像跨度后,如何防止光标在EditText(MultiAutoCompleteTextView)中调整大小? - How can I prevent the cursor from resizing in an EditText (MultiAutoCompleteTextView) after I add an imagespan using a SpannableStringBuilder? 我还如何使enter将edittext中的输入文本添加到列表视图? 在创建的添加按钮之上 - How can I also make enter add input text in edittext to my listview? On top of the created add button 如何在icon和editText下排很长的行,并在editText的末尾保留一些空间? - How can I have a long line under icon and editText and also leave some space at the end of editText? 使用自定义复选框选择器时,如何消除复选框按钮和文本之间的多余空间? - How can I get rid of extra space between the checkbox button and text when using a custom checkbox selector? 如何在 Kotlin 的 EditText 中的 4 个字符后添加空格? - How can I add space after 4 character in EditText at Kotlin? 我如何摆脱底部的空白区域? - How can I get rid of this empty white space on the bottom? Android EditText:如何摆脱图形故障? - Android EditText: How do I get rid of the graphical glitches? 文本在EditText中与ImageSpan混淆 - Text is messed up with ImageSpan in EditText 如何从另一个 XML 文件中获取 EditText 的文本? - How can i get the text of an EditText from another XML file? ZXing-如何将结果放入也用于手动输入的EditText中? - ZXing - How can I get the result into an EditText which is also used for manual entry?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM