触发View的measure()的原因 [英] What triggers a View's measure() to be called

查看:112
本文介绍了触发View的measure()的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我的View的onMeasure()覆盖之一有一个无限循环.从onMeasure的断点开始调试源代码,我可以一直跟踪到堆栈,一直跟踪到PhoneWindow $ DecorView的measure()(View层次结构中最顶层的类),ViewRoot会调用它. .performTraversals().现在从这里开始,如果我继续前进,最终将得到Looper.loop()类中的消息再次调用PhoneWindow $ DecorView的measure().我猜有些东西已经排队,需要重新测量,例如无效.

In my application I have an infinite loop on one of my View's onMeasure() overrides. Debugging the source code starting from a break point in my onMeasure, I am able to trace myself all the way up the stack trace up to the PhoneWindow$DecorView's measure() (top most class in my View Hierarchy), which gets called by ViewRoot.performTraversals(). Now from here if I keep stepping over, I eventually get the PhoneWindow$DecorView's measure() called again by a message in the Looper.loop() class. I'm guessing something has queued up a message that it needs to remeasure, like an invalidate.

我的问题是,什么触发条件调用需要在视图上发生?

My question is, what triggers that a measure call needs to occur on a View?

据我了解,布局/测量/绘制过程只会在特定视图上调用invalidate()方法时发生,并且会滴落并为无效的该视图执行布局/测量/绘制过程以及它的所有孩子我会以某种方式认为我在视图层次结构中最重要的视图正在失效.

From my understanding of the layout/measure/draw process, this will only occur when the invalidate() method is called on a specific view, and that will trickle down and perform a layout/measure/draw pass for that view invalidated and all of its children. I would assume that somehow my top most View in my View Hierarchy is getting invalidated.

但是,我已经在我拥有的每个无效调用上都明确指定了一个断点,而不是以某种无限的方式调用无效.因此,我认为情况并非如此.还有另一种触发度量通过的方法吗?内部是否会触发这种情况?看到没有什么是无限无效之后,我有点没主意了.

However, I've explicitly put a break point on every single invalidate call I have, and am not calling invalidate in some infinite manner. So I do not think that is the case. Is there another way to trigger a measure pass? Could something internally be triggering this? I'm kind of out of ideas after seeing that nothing is infinity invalidating.

推荐答案

为了触发自定义View的度量传递,必须调用requestLayout()方法. 例如,如果要实现扩展View的自定义视图,并且其行为类似于TextView,则可以编写如下的setText方法:

In order to trigger a measure pass for a custom View you must call the requestLayout() method. For example, if you are implementing a custom view that extends View and it will behave like a TextView, you could write a setText method like this:

/**
 * Sets the string value of the view.
 * @param text the string to write
 */
public void setText(String text) {
    this.text = text;

            //calculates the new text width
    textWidth = mTextPaint.measureText(text);

    //force re-calculating the layout dimension and the redraw of the view
    requestLayout();
    invalidate();
}

这篇关于触发View的measure()的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆