下面这篇是关于图文混排居中显示问题的解决方案。由于公司的直播项目的弹幕用到了文章中的
http://stackoverflow.com/questions/25628258/align-text-around-imagespan-center-vertical
CenteredImageSpan 但是图文确仍然无法实现居中对齐。
经过调试发现
int transY =( bottom - b.getBounds().bottom + // align bottom to bottom (drawableHeight - fontDescent + fontAscent) / 2)/5; // align center to center
transY 这个值初以 5 就能很好的实现图文居中对齐。
没有谷歌账户和其他账号也就发到这上面。希望能帮到其他人。
I have an ImageSpan
inside of a piece of text. What I've noticed is that the surrounding text is always drawn at the bottom of the text line -- to be more precise, the size of the text line grows with the image but the baseline of the text does not shift upward. When the image is noticeably larger than the text size, the effect is rather unsightly.
Here is a sample, the outline shows bounds of the TextView
:
I am trying to have the surrounding text be centered vertically with respect to the image being displayed. Here is the same sample with blue text showing the desired location:
Here are the constraints that I'm bound by:
- I cannot use compound drawables. The images must be able to be shown between words.
- The text may be multiline depending on the content. I have no control over this.
- My images are larger than the surrounding text and I cannot reduce their size. While the sample image above is larger than the actual images (to demonstrate the current behavior), the actual images are still large enough that this problem is noticeable.
I've tried using the android:gravity="center_vertical"
attribute on the TextView, but this does not have any effect. I believe this just vertically centers the text lines, but within the text line the text is still drawn at the bottom.
My current train of thought is to create a custom span that shifts the baseline of the text based on the height of the line and the current text size. This span would encompass the entire text, and I would have to compute the intersection with the ImageSpan
s so I can avoid shifting the images as well. This sounds rather daunting and I'm hoping someone can suggest another approach.
Any and all help is appreciated!