Android TextView 的启动将无法第一次工作,尺寸为零。秘诀是什么?
我已将视图充气到弹出窗口中。该视图包括一个 TextView,我想在运行时在其中放置文本。我需要计算 TextView 使用的行数,以便调整弹出窗口的大小。为此,我使用 TextView 的 getLineCount() 并在计算行数之前绘制 TextView ( myTextView.draw(canvas) )。
但是,第一次调用 PopupWindow 时,getLineCount() 返回字符数而不是行数(例如 42 而不是 2),调试 prits 表明这是因为 TextView 未正确启动:
DEBUG/View(207) : 框架={0, 0, 0, 0} 滚动={0, 0} 调试/视图(207): mMeasureWidth=0 mMeasureHeight=0 调试/视图(207): privateFlags={} DEBUG/View(207): frame={0, 0, 0, 0}scroll={0, 0} mText="要显示的文本.."
其余时间触发弹出窗口(由用户按下地图图标)视图具有非零参数,然后行计数工作正常:
DEBUG/View(207):frame={7, 13, 209, 79}scroll={0, 0} 调试/视图(207): mMeasureWidth=202 mMeasureHeight=66 调试/视图(207): privateFlags={HAS_BOUNDS} DEBUG/View(207): frame={7, 13, 209, 79}scroll={0, 0} mText="要显示的文本.."
作为解决方法,我尝试在绘制第一个之前绘制一个不可见的 PopupWindow弹出窗口,即尝试“第一次”使用它两次。这没有帮助..我还尝试使 MapView 无效,但这也不起作用。
任何人都可以建议如何正确启动视图,以便我可以按照预期的方式使用 getLineCount() 吗?
谢谢你! 瓦尼亚
I have inflated a View into a PopupWindow. This View includes a TextView where I want to place text at runtime. I need to count the number of lines the TextView uses in order to adjust the size of the pop-up. For this purpose I use TextView's getLineCount() and draw the TextView before counting the number of lines ( myTextView.draw(canvas) ).
However, the first time I call the PopupWindow the getLineCount() returns the number of characters and not the number of lines (e.g 42 instead of 2) Debug prits suggest this is because the TextView is not properly initiated:
DEBUG/View(207): frame={0, 0, 0, 0} scroll={0, 0}
DEBUG/View(207): mMeasureWidth=0 mMeasureHeight=0
DEBUG/View(207): privateFlags={}
DEBUG/View(207): frame={0, 0, 0, 0} scroll={0, 0} mText="text to display.."
The rest of the times a pop-up is triggered (by user press on map icons) the view has non-zero parameters, and then line count works fine:
DEBUG/View(207): frame={7, 13, 209, 79} scroll={0, 0}
DEBUG/View(207): mMeasureWidth=202 mMeasureHeight=66
DEBUG/View(207): privateFlags={HAS_BOUNDS}
DEBUG/View(207): frame={7, 13, 209, 79} scroll={0, 0} mText="text to display.."
As a work-around I tried to draw an invisible PopupWindow before drawing the first pop-up, i.e trying to use it "twice the first time". This didn't help.. I also tried to invalidate the MapView which didn't work either.
Can anyone please suggest how to initiate the views properly so that I can use getLineCount() the way it is supposed to?
Thank you!
Vanja
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为解决方法,我建议这个人的课程:
如何调整文本字体大小适合文本视图
我想它使用measureText() 可以很好地完成工作。
但为什么这不起作用 - 我不知道,必须深入研究它;)
我的建议是,一开始文本框非常窄,一行上只允许一个字符(这就是为什么它返回 42 而不是 2)。
as a workaround I would suggest this guy's class:
How to adjust text font size to fit textview
I guess it does the job fine, using measureText().
But why this doesn't work - I don't know, have to dig in it quite a bit ;)
My suggestion is that at first the text box is really narrow, and lets only one character on one line (this is why it returns 42 instead of 2).