简体   繁体   English

如何在Android中获取多行TextView的最大宽度?

[英]How to get maximum width of multiline TextView in android?

I am new in android and facing the problem to get the maximum width of the multiline TextView. 我是android的新手,面临着获取多行TextView的最大宽度的问题。

I used textview.getWidth() and textview.getMeasuredWidth() but both gives garbage value. 我使用了textview.getWidth()textview.getMeasuredWidth()但是两者都提供了垃圾值。

I used also textview.length() , but retuns zero. 我还使用了textview.length() ,但将其调整为零。

Please note that i have not set the maximum width of the TextView.I want to get it runtime. 请注意,我尚未设置TextView的最大宽度。我想让它运行。

TextView_name.setLayoutParams(new LayoutParams(250,LayoutParams.WRAP_CONTENT));

通过使用上面的代码,我们可以动态设置textview的宽度

I would try to use getWidth() . 我会尝试使用getWidth() If you want to do that though, you have to be careful when you call it. 但是,如果要执行此操作,则在调用它时必须小心。 It only makes sense after the view hierarchy is laid out. 仅在布局视图层次结构之后才有意义。 That means that trying to check onCreate , onStart or even onResume will probably not get the results that you want. 这意味着尝试检查onCreateonStart甚至onResume可能不会获得所需的结果。 As an experiment, you can call it in a click handler of a button and you should see the expected value correctly being returned. 作为实验,您可以在按钮的单击处理程序中调用它,并且应该看到正确返回了期望值。 I can't really say any more about where you should get it without knowing what you want to use it for. 在不知道要使用它的用途的情况下,我实在无法再说什么。

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

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