getWidth和getMeasuredWidth在何时可以得到正确数值(一)

2015-07-20 17:59:54 · 作者: · 浏览: 13

getMeasuredWidth在源码中的解释如下:

?

    /**
     * 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;
    }
大致意思也就是说返回最后一次调用onMeasure所测量得到的宽度。

?

?

getWidth在源码中返回View的宽度 单位是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;
    }

写一个Demo测试一下 代码如下:

?

?

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		MLinearLayout root = new MLinearLayout(this);
		setContentView(root);
	}
	
	class MLinearLayout extends LinearLayout
	{

		public MLinearLayout(Context 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:+getMeasuredWidth() + MeasuredHeight:+getMeasuredHeight());
		}
		
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			// TODO Auto-generated method stub
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
			Log.i(AAA,LinearLayout onMeasure Width:+getWidth() + Height:+getHeight());
			Log.i(AAA,LinearLayout onMeasure MeasuredWidth:+getMeasuredWidth() + MeasuredHeight:+getMeasuredHeight());
		}
		
		@Override
		protected void onLayout(boolean changed, int l, int t, int r, int b) {
			// TODO Auto-generated method stub
			super.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 context) {
			super(context);
			setLayoutParams(new LayoutParams(200, 300));
			setText(测试);
			Log.i(AAA, TextView MTextView Width:+getWidth() + Height:+getHeight());
			Log.i(AAA, TextView MTextView MeasuredWidth:+getMeasuredWidth() + MeasuredHeight:+getMeasuredHeight());
		}
		
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			// TODO Auto-generated method stub
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
			Log.i(AAA, TextView onMeasure Width:+getWidth() + Height:+getHeight());
			Log.i(AAA, TextView onMeasure MeasuredWidth:+getMeasuredWidth() + MeasuredHeight:+getMeasuredHeight());
		}