View.post(),和的Runnable执行时 [英] View.post(), and when Runnables are executed

查看:188
本文介绍了View.post(),和的Runnable执行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我原来的问题需要知道我的根查看的高度和宽度,使我可以让程序布局的变化。对于我而言,我并不一定需要确定在的onCreate该信息();这是相当足够的对我来说,编程根的布局完成后,加上我的孩子查看 S,所以因此我很乐意使用 onWindowFocusChanged() 作为一个钩子,以确定调用根时,查看的getWidth()的getHeight()将返回有效的结果。

My original problem was needing to know the height and width of my root View so that I could make programmatic layout changes. For my purposes, I don't necessarily need to determine this information during onCreate(); it's quite sufficient for me to programmatically add my child Views after layout of the root has completed, so therefore I'm happy to use onWindowFocusChanged() as a hook to determine when calling the root View's getWidth() and getHeight() will return valid results.

不过,我看到不少人都问如何确定根查看的过程中的onCreate大小()。我猜人们想为更好的用户体验做到这一点 - 也许使用户看不到的布局正在兴建中的阶段(如果有人能澄清的原因,我想AP preciate它)。现在,答案我已经常见的给这个,比如在这里的人 ,是发布的Runnable 来根查看的onCreate()。里面那个的Runnable 我们执行的getWidth()等。为什么这个工作的原因是因为,很显然,<$当查看的布局张贴到查看 C $ C>的Runnable 对象执行和/或附着到该窗口。

However, I see quite a few people have asked how to determine the root View's size during onCreate(). I'm guessing people want to do this for better user experience - perhaps so that users don't see the layout being built in stages (if anyone can clarify the reasons, I'd appreciate it). Now, the answer I have commonly seen given to this, such as the one here, is to post a Runnable to the root View within onCreate(). Inside that Runnable we perform the getWidth(), etc. The reason why this works is because, apparently, Runnable objects posted to a View are executed when the View has layout and / or is attached to the window.

这引出了我的问题。在查看 API文档,它似乎不来形容这种行为。那么,谁能告诉我它被定义和记录?或者说,它是检验源的问题?它是一个绝对的和严格的定义,功能,的Runnable 将留在查看的队列,只有在处理布局过程后,某一点?

This brings me to my question. In the API documentation for View, it doesn't seem to describe this behavior. So, can anyone tell me where it is defined and documented? Or, is it a matter of inspecting the source? Is it an absolutely and rigidly defined feature that Runnables will stay in a View's queue and only be handled at a certain point after the layout process?

进一步澄清:在读克里的答案,思前想多一点,我的基本问题都可以明确如下:在给出答案的这里以及在此的 $ C $的CProject进入的,据我所知,我们周围的根视图的<问题获得/ code>不是在现有尺寸的onCreate()通过发布的Runnable 查看。克里指出,保证的是,这个消息的Runnable 不能到的onCreate()已执行执行。据我所知,布局后的onCreate 出现了一些问题,但我仍然无法在此刻抓住为什么这个的Runnable 应当查看已知的尺寸执行。

Further clarification: On reading Kerry's answer and thinking it over a bit more, my basic question can be clarified as follows: In the answer given here and also in this CodeProject entry, I understand that we get around the problem of the root View's dimensions not being available during the onCreate() by posting a Runnable to the View. As Kerry points out, the guarantee is that this message Runnable cannot be executed until onCreate() has executed. I understand that layout occurs some point after onCreate, but I still can't grasp at the moment why this Runnable should execute when the View's dimensions are known.

推荐答案

我有一个看的 View.post(Runnable接口)方法,它在我看来,通过调用该方法刚刚添加的消息到UI线程队列。这将是'保证'的唯一的事情就是Runnable接口将已经称为后(Runnable接口)已经完成了方法之后的某个时候执行。这是假设你正在呼吁从UI线程后(Runnable接口),但作为文档说:

I had a look at the View.post(Runnable) method and it seems to me that by calling that method you are just adding a message to the UI thread queue. The only thing that is going to be 'guaranteed' is that the Runnable will be executed sometime AFTER the method which has called post(Runnable) has completed. This assumes you are calling post(Runnable) from the UI thread but as the docs say:

此方法只能从当这一观点被附加到窗口UI线程的外部调用。

This method can be invoked from outside of the UI thread only when this View is attached to a window.

我想,以确保无论您的根视图和子视图都有大小,你就需要调用后(Runnable接口)从孩子onSizeChanged()方法,因为如果孩子有大小,我认为它遵循父即因此,根视图必须有规模了。

I think to be sure that both your Root view and Child view both have size, you would need to call post(Runnable) from the child onSizeChanged() method because if the child has size I think it follows that the Parent i.e. Root view therefore must have size too.

道歉,如果这是一个有点散漫的回答。我有点明白你在想什么来实现,但不是100%肯定。我们如果你要我澄清什么我知道。

Apologies if this is a bit of a rambling answer. I kind of understand what you're wanting to achieve but not 100% sure. Let me know if you want me to clarify anything.

个人,每当我需要查看我onSizeChanged(),这我相信是这样做的正确的方式做的一切的规模和它一直工作。

Personally whenever I've needed the size of a View I do everything in onSizeChanged() which I do believe is the 'correct' way of doing it and it has always worked.

这篇关于View.post(),和的Runnable执行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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