简体   繁体   中英

How to get the width of a TextView?

I tried to resize an image inside a textview (using HTML layout and the image callback), but I could not get the correct width of the view. getWidth() always returns 0 for this view. I am calling the function inside onStart() in the application that contains the TextView.

Thanks for any hints!

RG

It is too early to call getWidth in onStart as the layout is not created yet. Try doing it in onMeasure .

PS. You may want to post some of your code as the question is not completely clear

You could pop this in onSizeChanged or in onLayout, these are called after the views are created.

Edit: the links seem to break the formatting...

http://developer.android.com/reference/android/view/View.html#onSizeChanged(int , int, int, int)

http://developer.android.com/reference/android/view/View.html#onLayout(boolean , int, int, int, int)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM