如何在自定义视图类中链接 onSizeChanged()、onMeasure()、onDraw()

Posted

技术标签:

【中文标题】如何在自定义视图类中链接 onSizeChanged()、onMeasure()、onDraw()【英文标题】:how to chain onSizeChanged(),onMeasure(),onDraw() in custom view class 【发布时间】:2011-08-01 10:28:46 【问题描述】:

以便我可以获得父宽度和父高度,以便我可以缩放我的图像以完全适合屏幕...请给任何合适的例子

【问题讨论】:

为什么不能使用match_parent,所以你知道,你可以匹配你的父母吗? 【参考方案1】:

将此添加到您的活动的 onCreate

ViewTreeObserver vto        =   layout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() 
            @Override
            public void onGlobalLayout() 
                You should be able to get the width and height
                over here.

                layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            
        );

【讨论】:

以上是关于如何在自定义视图类中链接 onSizeChanged()、onMeasure()、onDraw()的主要内容,如果未能解决你的问题,请参考以下文章