在我的代码中,我试图找到 textview 占用的高度..
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = new TextView(this);
ViewTreeObserver vto = t.getViewTreeObserver();
vto.addOnGlobalLayoutListener(this);
t.setText("This is to test the text\n Some Text \n Another ..");
t.append("\nafter something");
Toast.makeText(this, " The value at num = "+num,Toast.LENGTH_SHORT).show();
setContentView(t);
}
public void onGlobalLayout() {
// TODO Auto-generated method stub
num=t.getHeight();
Toast.makeText(this, "Hellooo !! height is "+num, Toast.LENGTH_SHORT).show();
}
onCreate() 中 num 的值始终为零,但最后在 onGlobalLayout() 中,正确的高度存储在 num 中,但我不能使用它,因为它是应用程序的末尾.. 有没有可能在应用程序中间获取文本长度?
请见谅,以上问题如有错误..
谢谢, 西瓦库姆尔
最佳答案
那不是应用程序的结束,那是 Android 完成布局的时候。如果您需要它作为应用程序的“中间”,请调用 onGlobalLayout()
一些将使用高度值的方法。参见 Event-driven programming .
关于java - 安卓 : Problem with OnGlobalLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4919328/