The reason we returned 0 when we called Getwith () and Getheigh () in OnCreate was that our view was not drawn at this time, not only in OnCreate. In the activity life cycle, the onstart,onresume,oncreate is not a real visible point in time, and the real visible time point is when the onwindowfocuschanged () function is executed.
So a workaround:
is to put the Getwith in Onwindowfocuschanged (). This onwindowfocuschanged refers to the time when the activity gets or loses focus and is called.
Another way is to register a Viewtreeobserver listener in OnCreate to listen to the view's drawing situation. This is an observer (Observer) that registers the listener view tree and is notified when the global event of the view tree is changed. This global event not only includes the entire tree layout, starting with the painting process, changing the touch pattern, and so on. The viewtreeobserver cannot be instantiated by the application because it is provided by the view.
He provides a lot of listeners, see: http://www.cnblogs.com/xingfuzzhd/archive/2013/06/25/3154674.html
Here's what we can do here:
//This can dynamically load the layout inside when the outer layout is complete /**out_layout is the Img_barde,tv_title,img_author, the outer layout, the layout of the outer layer after loading is completed according to the img_barde,tv_title of the high-width dynamic drawing img_author. Viewtreeobserver viewtreeobserver = Out_layout.getviewtreeobserver ();/** Registers a callback function that calls this callback function when a view tree is about to be drawn. * Parameter listener the callback function that will be added * exception illegalstateexception if IsAlive () returns false */Viewtreeobserver.addonpredrawlistener (NewViewtreeobserver.onpredrawlistener () {@Override Public Boolean Onpredraw() {if(!hasmeasured) {Framelayout.layoutparams Layoutparams =NewFramelayout.layoutparams (framelayout. Layoutparams.match_parent, FrameLayout.LayoutParams.WRAP_CONTENT); Layoutparams.setmargins (0, Img_bar.getheight () + tv_title.getheight (),0,0); Img_author.setlayoutparams (Layoutparams); hasmeasured =true; }return true; } });
View's GetWidth () getheight () always returns several workarounds for 0