Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓:OnGlobalLayout的问题_Java_Android_Textview - Fatal编程技术网

Java 安卓:OnGlobalLayout的问题

Java 安卓:OnGlobalLayout的问题,java,android,textview,Java,Android,Textview,在我的代码中,我试图找到textview占据的高度 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); t = new TextView(this); ViewTreeObserver vto = t.getViewTreeObserver(); vto.addOnGlob

在我的代码中,我试图找到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中,但我无法使用它,因为它是应用程序的结尾。。 在应用程序的中间是否有可能获得文本长度?

如果上述问题中有任何错误,请原谅

谢谢,
Siva Kuamr

这不是应用程序的结束,而是Android完成布局的时候。如果需要将其作为应用程序的“中间”,请调用将使用height值的方法。请参阅。

这不是应用程序的结束,而是Android完成布局的时候。如果需要将其作为应用程序的“中间”,请调用将使用height值的方法。请参阅。

有解决方案吗???有解决方案吗???