开发者

Initiation of Android TextView will not work first time, dimensions are zero. What is the secret trick?

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/Vie开发者_如何转开发w(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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜