如何获取android中多行TextView的最大宽度?
我是 android 新手,面临着获取多行 TextView 的最大宽度的问题。
我使用了 textview.getWidth()
和 textview.getMeasuredWidth()
但两者都给出了垃圾值。
我还使用了 textview.length()
,但返回零。
请注意,我还没有设置 TextView 的最大宽度。我想在运行时获取它。
I am new in android and facing the problem to get the maximum width of the multiline TextView.
I used textview.getWidth()
and textview.getMeasuredWidth()
but both gives garbage value.
I used also textview.length()
, but retuns zero.
Please note that i have not set the maximum width of the TextView.I want to get it runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用上面的代码我们可以动态设置textview的宽度
by using the above code we can set the width for textview dynamically
我会尝试使用
getWidth()
。但如果你想这样做,那么调用它时必须小心。只有在布局视图层次结构之后它才有意义。这意味着尝试检查onCreate
、onStart
甚至onResume
可能不会得到您想要的结果。作为实验,您可以在按钮的单击处理程序中调用它,并且您应该看到正确返回的预期值。在不知道你想用它做什么的情况下,我真的无法再多说你应该从哪里得到它。I would try to use
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 checkonCreate
,onStart
or evenonResume
will probably not get the results that you want. 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.