设为首页 加入收藏

TOP

getWidth和getMeasuredWidth在何时可以得到正确数值(一)
2015-07-20 17:59:54 来源: 作者: 【 】 浏览:7
Tags:getWidth getMeasuredWidth 何时 可以 得到 正确 数值

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());
		}
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇算法怎么就这么难---归并排序和快.. 下一篇ListView中pointToPosition()方法..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: