简体   繁体   中英

OnWindowFocusChanged vs OnCreate for assigning object properties

My question revolves around the onWindowFocusChanged method. I know that as a best practice, you should inflate your objects in the onCreate method.

What I'm wondering about is this: objects such as ImageViews are not completely rendered during the onCreate method. So you use onWindowFocusChanged, which the Android documentation says is called once the activity render is complete. I have ImageView objects that I need to get the X and Y positions for and (because of varying screen sizes) I won't know exactly where they are positioned on the screen unless I use hard coded numbers, which I don't want to do. I'll then assign margins for other ImageViews based on these X and Y numbers. But if I have to wait until onWindowFocusChanged to get X and Y, it doesn't do me any good to set those numbers in onCreate method.

So my question is - can I still set the properties of an ImageView inside of onWindowFocusChanged and have them re-rendered to their updated properties, or are the property values locked in because they've already been rendered?

Try by maintaining your View object inside the Activity class and calling postInvalidate() after the properties are set inside onWindowFocusChanged(). Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM