无法获取小部件的高度 [英] Can't get height of Widgets

查看:71
本文介绍了无法获取小部件的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取TextView的高度/宽度时,我遇到了一个问题.

I am facing a problem when gets height/width of TextView.

我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv1 = (TextView)findViewById(R.id.textview1);
    int h1 = tv1.getHeight();
    TextView tv2 = (TextView)findViewById(R.id.textview2);
    int h2 = tv2.getHeight();
}

h1是textview1的高度h2是textview2的高度

h1 is height of textview1 h2 is height of textview2

这些h1和h2始终等于0.

These h1 and h2 are always equal to 0.

这是怎么了?

请咨询.

谢谢.

推荐答案

View.getWidth()/View.getHeight()在测量视图之前不会给您有意义的答案.在此处了解有关 Android如何绘制视图的信息.
参见此 thread 了解何时可以使用测量尺寸.

View.getWidth()/View.getHeight() won't give you a meaningful answer until the view has been measured. Read about how android draws views here.
See this thread to get an idea of when the measured dimensions will be available.

这篇关于无法获取小部件的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆