开发者

Which Activity method is called after all the layout methods?

开发者 https://www.devze.com 2023-04-06 10:51 出处:网络
I need to do something in an Activity after all the layout meth开发者_如何学JAVAods have been called, all the Views are in place and the Activity is ready to be displayed.

I need to do something in an Activity after all the layout meth开发者_如何学JAVAods have been called, all the Views are in place and the Activity is ready to be displayed.

Which method can do that?


If you are trying to get a width of a view or something. This should work

Add this to your activity's onCreate

ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        // Put your code here. 

        layout.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
    } 
}); 


There's no magic method for that AFAIK. Suggest adding a Handler to your activity class, and post() a Runnable to it from onCreate() that contains the code you want to run.

If that's still too early you can postDelayed() instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消