问题描述
我创建了一个自定义布局延伸的ViewGroup。一切工作正常,我得到预期的布局。
I have created a custom layout which extends ViewGroup. Everything is working fine and I am getting the layout as expected.
欲动态改变在布局中的一个元素。然而,这是行不通的,因为我调用它的onCreate和到那时,整个布局是不实际(画)膨胀到屏幕上,因此没有实际尺寸。
I want to dynamically change an element in the layout. However this is not working as I am calling it in onCreate and till that time the entire layout is not actually (drawn) inflated onto the screen and hence do not have actual size.
有没有可以用来找出什么时候布局的通胀做任何活动吗?我想onFinishInflate但不会作为工作的ViewGroup有多种意见,这将被多次调用。
Is there any event which can be used to find out when the inflation of the layout is done? I tried onFinishInflate but that would not work as Viewgroup has multiple views and this would be called multiple times.
我想创建一个界面中的自定义布局类,但不知道什么时候火呢?
I am thinking of creating an interface in the Custom layout class but not sure when to fire it?
推荐答案
如果我正确地理解您的需求,一个OnGlobalLayoutListener可以给你所需要的。
If I understand your requirements correctly, an OnGlobalLayoutListener may give you what you need.
View myView=findViewById(R.id.myView);
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//At this point the layout is complete and the
//dimensions of myView and any child views are known.
}
});
这篇关于ViewGroup中完成充气事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!