When can the getwidth and getmeasuredwidth get the correct value?

Source: Internet
Author: User

Tag: getwidth getmeasuredwidth onmeasure onlayout

Getmeasuredwidth in the source code is explained as follows:

    /**     * Like {@link #getMeasuredWidthAndState()}, but only returns the     * raw width component (that is the result is masked by     * {@link #MEASURED_SIZE_MASK}).     *     * @return The raw measured width of this view.     */    public final int getMeasuredWidth() {        return mMeasuredWidth & MEASURED_SIZE_MASK;    }    /**     * Return the full width measurement information for this view as computed     * by the most recent call to {@link #measure(int, int)}.  This result is a bit mask     * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.     * This should be used during measurement and layout calculations only. Use     * {@link #getWidth()} to see how wide a view is after layout.     *     * @return The measured width of this view as a bit mask.     */    public final int getMeasuredWidthAndState() {        return mMeasuredWidth;    }
That is to say, return the width measured by the last onmeasure call.


Getwidth in the source code, the unit of view width is pixels.

/**     * Return the width of the your view.     *     * @return The width of your view, in pixels.     */    @ViewDebug.ExportedProperty(category = "layout")    public final int getWidth() {        return mRight - mLeft;    }

Write a demo and test the Code as follows:

Public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); mlinearlayout root = new mlinearlayout (this); setcontentview (Root);} class mlinearlayout extends linearlayout {public mlinearlayout (context) {super (context ); setlayoutparams (New layoutparams (layoutparams. fill_parent, layoutparams. match_parent); addview (New mtextview (context); log. I ("AAA", "linearlayout mlinearlayout width:" + getwidth () + "height:" + getheight (); log. I ("AAA", "linearlayout mlinearlayout measuredwidth:" + Week () + "measuredheight:" + getmeasuredheight ();} @ overrideprotected void onmeasure (INT widthmeasurespec, int week) {// todo auto-generated method stubsuper. onmeasure (widthmeasurespec, heightmeasurespec); log. I ("AAA", "linearlayout onmeasure width:" + getwidth () + "height:" + getheight (); log. I ("AAA", "linearlayout onmeasure measuredwidth:" + getmeasuredwidth () + "measuredheight:" + getmeasuredheight ();} @ overrideprotected void onlayout (Boolean changed, int l, int T, int R, int B) {// todo auto-generated method stubsuper. onlayout (changed, L, t, R, B); log. I ("AAA", "linearlayout onlayout width:" + getwidth () + "height:" + getheight (); log. I ("AAA", "linearlayout onlayout measuredwidth:" + getmeasuredwidth () + "measuredheight:" + getmeasuredheight ();} class mtextview extends textview {public mtextview (context) {super (context); setlayoutparams (New layoutparams (200,300); settext ("test"); log. I ("AAA", "textview mtextview width:" + getwidth () + "height:" + getheight (); log. I ("AAA", "textview mtextview measuredwidth:" + measures () + "measuredheight:" + getmeasuredheight () ;}@ overrideprotected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {// todo auto-generated method stubsuper. onmeasure (widthmeasurespec, heightmeasurespec); log. I ("AAA", "textview onmeasure width:" + getwidth () + "height:" + getheight (); log. I ("AAA", "textview onmeasure measuredwidth:" + getmeasuredwidth () + "measuredheight:" + getmeasuredheight ();} @ overrideprotected void onlayout (Boolean changed, int left, int top, int right, int bottom) {// todo auto-generated method stubsuper. onlayout (changed, left, top, right, bottom); log. I ("AAA", "textview onlayout width:" + getwidth () + "height:" + getheight (); log. I ("AAA", "textview onlayout measuredwidth:" + getmeasuredwidth () + "measuredheight:" + getmeasuredheight ());}}}

The result is as follows:

I/AAA (3631): textview mtextview width: 0 Height: 0
I/AAA (3631): textview mtextview measuredwidth: 0 measuredheight: 0
I/AAA (3631): linearlayout mlinearlayout width: 0 Height: 0
I/AAA (3631): linearlayout mlinearlayout measuredwidth: 0 measuredheight: 0
I/AAA (3631): textview onmeasure width: 0 Height: 0
I/AAA (3631): textview onmeasure measuredwidth: 200 measuredheight: 300
I/AAA (3631): linearlayout onmeasure width: 0 Height: 0
I/AAA (3631): linearlayout onmeasure measuredwidth: 1024 measuredheight: 502
I/AAA (3631): textview onlayout width: 200 Height: 300
I/AAA (3631): textview onlayout measuredwidth: 200 measuredheight: 300
I/AAA (3631): linearlayout onlayout width: 1024 Height: 502
I/AAA (3631): linearlayout onlayout measuredwidth: 1024 measuredheight: 502
I/AAA (3631): textview onmeasure width: 200 Height: 300
I/AAA (3631): textview onmeasure measuredwidth: 200 measuredheight: 300
I/AAA (3631): linearlayout onmeasure width: 1024 Height: 502
I/AAA (3631): linearlayout onmeasure measuredwidth: 1024 measuredheight: 502
I/AAA (3631): textview onlayout width: 200 Height: 300
I/AAA (3631): textview onlayout measuredwidth: 200 measuredheight: 300
I/AAA (3631): linearlayout onlayout width: 1024 Height: 502
I/AAA (3631): linearlayout onlayout measuredwidth: 1024 measuredheight: 502

Conclusion:

1. In the constructor, neither getwidth nor getmeasuredwidth can obtain the correct value.

2. Get the correct value by getmeasuredwidth after onmeasure is called.

3. To get the correct value through getwidth, you must call onlayout.


Drawing Process of view: http://www.cnblogs.com/cowboybusy/archive/2012/08/26/2718888.html


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.