View's GetWidth () getheight () always returns several workarounds for 0

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.