java - 安卓 : Problem with OnGlobalLayout

标签 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.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/

相关文章:

java - jTabbedPane 在加载时打开不同的选项卡

java - 通过 vb.net 将焦点设置为 webbrowser 内的 java-inputdialog

android - ViewModel 类实现 LifecycleObserver 可以吗?

android - iOS 自定义 URL 方案与通用链接和 Android 对应方案

android - 动态添加到表行时 ImageButton 不可见

Java获取name属性的值

java - 全文搜索 api 无法与 objectify java : index. put() 一起使用,抛出 nosuchmethod

java - 为android编写一个超时事件

android - 在 Android 多行 TextView 中禁用自动换行

android - 如何在 Android UI 中绘制圆角矩形?