简体   繁体   English

如何获取TextView的宽度?

[英]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. 我试图在textview中调整图像的大小(使用HTML布局和图像回调),但无法获得正确的视图宽度。 getWidth() always returns 0 for this view. 此视图的getWidth()始终返回0。 I am calling the function inside onStart() in the application that contains the TextView. 我在包含TextView的应用程序中的onStart()内部调用函数。

Thanks for any hints! 感谢您的提示!

RG RG

It is too early to call getWidth in onStart as the layout is not created yet. 由于尚未创建布局,因此在onStart调用getWidth还为时过早。 Try doing it in onMeasure . 尝试在onMeasure

PS. 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. 您可以在onSizeChanged或onLayout中将其弹出,创建视图后将调用它们。

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#onSizeChanged(int,int,int,int

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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