zoukankan      html  css  js  c++  java
  • Android在onCreate中获取控件的宽高

    在某些需求下,我们需要在onCreate的时候就获取到控件的宽高,但是如果直接用view.getWidth()或view.getHeight()会得到0.这是因为在onCreate执行的时候,控件还没有被绘制出来.

    利用下面的方法可以获得控件的宽高:

     ViewTreeObserver vto = zoomImageView.getViewTreeObserver();
            vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    if (hasMeasured == false)
                    {
    
                        int height = zoomImageView.getMeasuredHeight();
                        int width = zoomImageView.getMeasuredWidth();
                        Log.i("height",height+"");
                        Log.i("width",width+"");
                        hasMeasured = true;
                    }
                    return true;
                }
            });
    作者:caobotao
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    标准输入输出流
    打印流
    数据输入输出流
    对象操作流
    随机流
    内存输出流
    序列流
    转换流示例代码
    字符流的示例代码
    字符流
  • 原文地址:https://www.cnblogs.com/caobotao/p/5041586.html
Copyright © 2011-2022 走看看