安卓:编写自定义视图支持消息队列 [英] Android: Writing Custom Views that Support The Message Queue

查看:129
本文介绍了安卓:编写自定义视图支持消息队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现在Android中自定义视图,使其正确支持,或工作,消息队列?

How do you implement a custom view in Android so that it properly supports, or works with, the messaging queue ?

我试图仿效的内置视图的行为,这样我可以正常/常更新数据中的onCreate自定义视图。

I'm trying to emulate the behavior of the built-in views so that I can properly / normally update a custom view with data within onCreate.

目前,我的自定义视图有特设的设置/更新功能将数据在其中。这里的问题是,我的观点的孩子的意见不被初始化,直到第一次onMeasure被调用,这是退出的onCreate(我认为这是内置插件如何做到这一点)之后。

Currently, my custom view has ad-hoc set/update functions to put data in them. The problem with this is that my view's children views are not initialized until the first time onMeasure is called, which is after onCreate exits (which I think is how the built-ins do it).

所以,我想知道一般的策略是什么(即覆盖哪些方法),以更新的onCreate自定义视图以这样一种方式,更新进入消息队列和到达的观点后,他们被正确实例(就像内置插件)?

Therefore, I want to know what the general strategy is (ie, what methods to override) to update a custom view from onCreate in such a way that the updates go into the message queue and reach the view after they are properly instantiated (just like the built-ins) ?

感谢。

推荐答案

看的 View.post()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView hello = ((TextView) findViewById(R.id.hello));
    hello.post(new Runnable() {
        @Override
        public void run() {
            hello.setText("Hello World!");
        }
    });
}

这篇关于安卓:编写自定义视图支持消息队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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